# coderbyte代码竞赛小测试

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.

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.

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.

04

LetterCapitalize

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

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.

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.

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”

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

2019-02-23

2019-02-23

2019-02-23

2019-02-23

2019-02-23

2019-02-23