欢迎光临!
若无相欠,怎会相见

Python习题之第二天

题目

直接进入主题。本次练习了三题,题目如下:

题一: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时,把他们变成大写字母。相当于一个简单的加密。需要用到内置函数:

  1. isalpha() : 判断是否是字母
  2. ord() : 字符转换成ASCII码
  3. 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

版权声明:  本站原创文章,于 2018-05-08,由 Darker 发表。
转载请注明:原文地址:《Python习题之第二天 | 飘零博客》
本站所有文章如果没有特殊声明均为原创。本文作者 : Darker
复制或转载请以超链接形式注明:飘零博客    
原文地址:《Python习题之第二天 | 飘零博客》
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
//

欢迎光临