题目
直接进入主题。本次练习了三题,题目如下:
题一:Letter Changes
Challenge
Using the Python language, have the function LetterChanges(str)
take the str
parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie.
c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
Sample Test Cases
Input:"hello*3"
Output:"Ifmmp*3"
Input:"fun times!"
Output:"gvO Ujnft!"
Hint
Changing a character to the next one that appears in the alphabet can easily be achieved by using the letters character code (ASCII).
题二:Simple Adding
Challenge
Using the Python language, have the function SimpleAdding(num)
add up all the numbers from 1 to num
. For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter num
will be any number from 1 to 1000.
Sample Test Cases
Input:12
Output:78
Input:140
Output:9870
Hint
There’s a very simple formula to calculate the sum of the first N numbers.
题三:Letter Capitalize
Challenge
Using the Python language, have the function LetterCapitalize(str)
take the str
parameter being passed and capitalize the first letter of each word. Words will be separated by only one space.
Sample Test Cases
Input:”hello world”
Output:”Hello World”
Input:”i ran there”
Output:”I Ran There”
Hint
There might be a built-in function in your programming language that capitalizes the first letter of each word.
分析
第一题就是一个字母变换的问题。在一个字符串中,除了字母会变换,数字和符号都不变。其中,”z”会变成”a”,其他字母都是ASCII码加一,当变换后的字母是a、e、i、o、u时,把他们变成大写字母。相当于一个简单的加密。需要用到内置函数:
- isalpha() : 判断是否是字母
- ord() : 字符转换成ASCII码
- chr() : ASCII码转换成字符
第二题就是一个连加运算。
第三题是把字符串中的单词首字母变成大写。
代码
题一:Letter Changes
def LetterChanges(str):
# code goes here
new_str = ''
for char in str:
if char.isalpha():
if char == 'z':
char = 'a'
else:
char = chr(ord(char)+1)
if char in 'aeiou':
char = char.upper()
new_str = new_str + char
str = new_str
return str
# keep this function call here
print LetterChanges(raw_input())
题二:Simple Adding
def SimpleAdding(num):
# code goes here
s = 0
for i in range(1,num+1):
s += i
num = s
return num
# keep this function call here
print SimpleAdding(raw_input())
题三:Letter Capitalize
def LetterCapitalize(str):
# code goes here
words = str.split()
for i in range(0,len(words)):
words[i] = words[i][0].upper()+words[i][1:]
str = ' '.join(words)
return str
# keep this function call here
print LetterCapitalize(raw_input())
上述代码我都已经在CoderByte上测试过,所以我就不贴上图片了!
结语
我感觉把这些题目做成PDF文件会比较完美,因此,我会尽快把题目完成!
PS:今天更新了一下网站程序,结果得重新改代码,什么文章草稿、修订版之类的,然后改完没有测试,我当时的想法是就拿这篇文章测试的,结果出现“500”错误,文章直接丢失,害的我重新写这篇文章。不过还好,思路还保留着,不用太多时间。
老规矩:如有错误,敬请指出,感谢指正! — 2018-05-08 23:08:46
最新评论
这个软件有bug的,客户端windows有些键不能用如逗号、句号
没有收到邮件通知
我的评论通知貌似坏掉了,定位一下问题
测试一下重新部署后的邮件功能
居然看到自己公司的MIB库,诚惶诚恐
那可能是RobotFramework-ride的版本问题。我装的1.7.4.2,有这个限制。我有空再尝试下旧版本吧,感谢回复。
你好!我在python2.7中安装RobotFramework-ride的时候提示wxPython的版本最高是2.18.12,用pip下载的wxPython版本是4.10,而且我在那个路径下没有找到2
真的太好了,太感谢了,在bilibili和CSDN上都找遍了,终于在你这里找到了