Google python course basic exercise——string1

这是google python课程里的练习题一——string 1,通过这几个小程序的实现,足以窥一斑而知全豹,发现python的美。

题目及代码如下:

# A. donuts
# Given an int count of a number of donuts, return a string
# of the form 'Number of donuts: <count>', where <count> is  the number
# passed in. However, if the count is 10 or more, then use the word 'many'
# instead of the actual count.
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
    if 0<count<10:      #这里注意哦,只有python等少数语言能这样写判断表达式,C/C++和java都是没有的。
        word = count
    elif count>=10:
        word = 'many'
    else:
        print 'please enter the positive number!'
    return 'Number of donuts: %s'% word


# B. both_ends # Given a string s, return a string made of the first 2 # and the last 2 chars of the original string, # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): if len(s)<2: word = '' else: word = '%s%s%s%s' % (s[0],s[1],s[-2],s[-1]) #这里[]内的"-"符号表示逆序 return word

# C. fix_start # Given a string s, return a string # where all occurences of its first char have # been changed to '*', except do not change # the first char itself. # e.g. 'babble' yields 'ba**le' # Assume that the string is length 1 or more. # Hint: s.replace(stra, strb) returns a version of string s # where all instances of stra have been replaced by strb. def fix_start(s): c = s[0] a = s.replace(c,'*') b = c+a[1:] return b

# D. MixUp # Given strings a and b, return a single string with a and b separated # by a space '<a> <b>', except swap the first 2 chars of each string. # e.g. # 'mix', pod' -> 'pox mid' # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a,b): if len(a)<2 or len(b)<2: print "please enter the string whose length is > 2" else: c = b[0:2]+a[2:] d = a[0:2]+b[2:] word = '%s %s' % (c,d) return word

# Provided simple test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if got == expected: prefix = ' OK ' else: prefix = ' X ' print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))

# Provided main() calls the above functions with interesting inputs, # using test() to check if each result is correct or not. def main(): print 'donuts' # Each line calls donuts, compares its result to the expected for that call. test(donuts(4), 'Number of donuts: 4') test(donuts(9), 'Number of donuts: 9') test(donuts(10), 'Number of donuts: many') test(donuts(99), 'Number of donuts: many') print print 'both_ends' test(both_ends('spring'), 'spng') test(both_ends('Hello'), 'Helo') test(both_ends('a'), '') test(both_ends('xyz'), 'xyyz') print print 'fix_start' test(fix_start('babble'), 'ba**le') test(fix_start('aardvark'), 'a*rdv*rk') test(fix_start('google'), 'goo*le') test(fix_start('donut'), 'donut') print print 'mix_up' test(mix_up('mix', 'pod'), 'pox mid') test(mix_up('dog', 'dinner'), 'dig donner') test(mix_up('gnash', 'sport'), 'spash gnort') test(mix_up('pezzy', 'firm'), 'fizzy perm') # Standard boilerplate to call the main() function. if __name__ == '__main__': main()

运行结果:

donuts
OK got: 'Number of donuts: 4' expected: 'Number of donuts: 4' 
OK got: 'Number of donuts: 9' expected: 'Number of donuts: 9' 
OK got: 'Number of donuts: many' expected: 'Number of donuts: many' 
OK got: 'Number of donuts: many' expected: 'Number of donuts: many' 

both_ends
OK got: 'spng' expected: 'spng' 
OK got: 'Helo' expected: 'Helo' 
OK got: '' expected: '' 
OK got: 'xyyz' expected: 'xyyz' 

fix_start
OK got: 'ba**le' expected: 'ba**le' 
OK got: 'a*rdv*rk' expected: 'a*rdv*rk' 
OK got: 'goo*le' expected: 'goo*le' 
OK got: 'donut' expected: 'donut' 

mix_up
OK got: 'pox mid' expected: 'pox mid' 
OK got: 'dig donner' expected: 'dig donner' 
OK got: 'spash gnort' expected: 'spash gnort' 
OK got: 'fizzy perm' expected: 'fizzy perm'

 

另外需说明的是c项fix_start()函数中: 

  1. 调用s.replace()函数得到的是s修改的的副本,原串s保持不变,故用另一变量接收。
  2. 得到变量a后很容易想到的下一步动作是 b=a.replace(a[0],c),但运行后发现b=s了,可见此处a.replace(a[0],c)执行的效果并不是将串a中第一位a[0]替换成c字符,而是将串a中所有匹配a[0]的字符替换成了c,这就导致两次替换后,还原了。eg:s="google";c=s[0];a=s.replace(c,"*");b=a.replace(a[0],c)以为可以得到'goo*le',结果得到'google'。所以我采用了字符串的连接方法。
posted @ 2013-03-29 17:41  Emma437211  阅读(381)  评论(0编辑  收藏  举报