coderbyte代码竞赛小测试

利用零碎时间,上coderbyte编程竞赛网站做了些练习,初中高难度都尝试了下,全部使用python语言,实现难度不大,测试时有时间要求。现将其贴出来供大家讨论,如果有更高效的实现方式,欢迎留言

01

Longest Word

题目:

Have the functionLongestWord(sen)take thesenparameter being passed and returnthe largest word in the string. If there are two or more words that are thesame length, return the first word from the string with that length. Ignorepunctuation and assumesenwill not be empty.

思路:

用正则式匹配,返回单词list,最关键的是如何使用pattern模式串

代码:

02

FirstReverse

题目:

Have thefunction FirstReverse(str) take the str parameter being passed and returnthe string in reversed order. For example: if the input string is "HelloWorld and Coders" then your program should return the string sredoC dnadlroW olleH.

思路:

将字符串用生成器输出成列表,逆序进行字符串拼接,注意逆序拼接时是-1到-len(ls),代码应该是range(1,len(ls)+1)

代码:

03

LetterChange

题目:

Havethe function take the parameter being passed and modify it using the followingalgorithm. Replace every letter in the string with the letter following it inthe alphabet ( c becomes d, z becomes a). Then capitalize every vowel in thisnew string (a, e, i, o, u) and finally return this modified string.

思路:

首先将要元音字母用列表表示,然后使用map函数,结合char(ascii转化为str)和ord(str转化为ascii)函数,采用生成器生成字符列表,然后采用生成器的形式实现小写变大写,最后对新列表进行字符串拼接输出

代码:

04

LetterCapitalize

题目:

Have thefunctionLetterCapitalize(str)take thestrparameter being passed andcapitalize the first letter of each word. Words will be separated by only onespace.

思路:

先拆分字符串成list,然后对每个word调用capitalize()方法,最后用join方法直接将ls_str这个word列表进行拼接返回(ls_str可迭代的)

代码:

05

AlphabetSoup

题目:

Havethe function take the string parameter being passed and return the string withthe letters in alphabetical order ( hello becomes ehllo). Assume numbers andpunctuation symbols will not be included in the string.

思路:

map和sorted方法搭配起来,简直无敌!

代码:

06

SimpleSymbols

题目:

Havethe function take the parameter being passed and determine if it is anacceptable sequence by either returning the string or . The parameter will becomposed of and symbols with several letters between them ( ++d+===+c++==a) andfor the string to be true each letter must be surrounded by a symbol. So thestring to the left would be false. The string will not be empty and will haveat least one letter.

思路:

对str遍历,如果i是字符或是数字,正则匹配寻找”+。。。Alpha。。+“形式的字符串,一旦没匹配成功,就退出程序,返回False,否则直至遍历完毕,返回True,说明所有字符或数字均满足要求的模式

代码:

07

KaprekarsConstant

题目:

Using the Python language, have the function KaprekarsConstant(num) take the num parameter being passed which will bea 4-digit number with at leasttwo distinct digits. Your program should performthe following routine on the number: Arrange the digits in descending order andin ascending order (adding zeroes to fit it to a 4-digit number), and subtractthe smaller number from the bigger number. Then repeat the previous step.Performing this routine will always cause you to reach a fixed number: 6174.Then performing the routine on 6174 will always give you 6174 (7641 - 1467 =6174). Your program should return the number of times this routine must beperformed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1)5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174

关键点:

1、raw_input()在python2.x中支持,python3.x更改为input(),返回值是str类型

2、关于整数除法,python3中支持//操作符,python2支持/操作符

3、str和int类型之间不能直接使用==比较

4、利用str.zfill对不足四位的输入补齐四位数;

5、sorted支持所有可迭代类型的排序,以reverse关键词为True可以降序排序;而sort只支持对list的排序

代码:

08

ChessBoardTraveling

题目:

Havethe function read which will be a string consisting of the location of a spaceon a standard 8x8 chess board with no pieces on the board along with anotherspace on the chessboard. The structure of will be the following: "(x y)(ab)" where (x y) represents the position you are currently on with x and yranging from 1 to 8 and (a b) represents some other space on the chess boardwith a and b also ranging from 1 to 8 where a > x and b > y. Your programshould determine how many ways there are of traveling from (x y) on the boardto (a b) moving only up and to the right. For example: if is then your programshould output because there are only two possible ways to travel from space (11) on a chessboard to space (2 2) while making only moves up and to the right.

思路:

这是一个典型的动态规划问题,自定义一个递归findpath函数即可。此处要注意,elif这段代码不能漏掉,一旦漏掉将会搜索整个棋盘空间(预先的向右向上寻径失效),从而会导致超过python默认的最大递归深度(当然,默认递归深度是可以更改的,具体可以上网了解)

代码:

09

QuestionMarks

题目:

Havethe function take the string parameter, which will contain single digitnumbers, letters, and question marks, and check if there are exactly 3 questionmarks between every pair of two numbers that add up to 10. If so, then yourprogram should return the string , otherwise it should return the string . Ifthere aren't any two numbers that add up to 10 in the string, then your programshould return as well. For example: if is "arrb6???4xxbl5???eee5"then your program should return because there are exactly 3 question marksbetween 6 and 4, and 3 question marks between 5 and 5 at the end of the string.

思路:

1、首先自定义一个判断数字之和是否大于10以及索引之间问号个数是否等于3的函数detect_ques_mark();

2、遍历一遍字符串,将数字的下标索引全部添加到ls_index中

3、从ls_index中遍历数字对,调用detect_ques_mark()函数,一旦发现不符合要求的,直接退出返回“false”,否则遍历到最后返回“true”

代码存在问题:

特殊情况考虑不够,比如输入为"aaaaaaarrrrr??????"时,输出结果不正确,不过加一个特殊情况检测即可

代码:

总体心得:

1、python代码确实简洁

2、python的生成器很有用

3、python的re正则包在处理字符串时相当有用

4、python3.x与python2.x有较大区别:

(1)3.x整除操作符为//,2.x为/

(2)3.x键盘输入为input,2.x为raw_input

(3)3.6才开始支持f"..."形式的f字符串,3.6以前都是‘...’.format()

还有很多.....

  • 发表于:
  • 原文链接:http://kuaibao.qq.com/s/20180126G03VAM00?refer=cp_1026

相关快讯

扫码关注云+社区