Google python course basic exercise——string2

在string1之后继续推出string2:

View Code
 1 # -*- coding: cp936 -*-
 2 # D. verbing
 3 # Given a string, if its length is at least 3,
 4 # add 'ing' to its end.
 5 # Unless it already ends in 'ing', in which case
 6 # add 'ly' instead.
 7 # If the string length is less than 3, leave it unchanged.
 8 # Return the resulting string.
 9 def verbing(s):
10     if len(s)>=3:
11         if s[-3:] == 'ing':
12             s = s + 'ly'
13         else:
14             s = s + 'ing'
15     return s
16 
17 # E. not_bad
18 # Given a string, find the first appearance of the
19 # substring 'not' and 'bad'. If the 'bad' follows
20 # the 'not', replace the whole 'not'...'bad' substring
21 # with 'good'.
22 # Return the resulting string.
23 # So 'This dinner is not that bad!' yields:
24 # This dinner is good!
25 def not_bad(s):
26     if 'not' in s and 'bad' in s: #先判断有没有,如果去掉此句,则当s中没有not有bad时会出错
27             i = s.find('not')
28             j = s.find('bad')
29             if i<j:             #强调首次出现时not在前,bad在后
30                 s = s[0:i]+'good'+s[j+3:]  #这样替换
31     return s
32 
33 
34 # F. front_back
35 # Consider dividing a string into two halves.
36 # If the length is even, the front and back halves are the same length.
37 # If the length is odd, we'll say that the extra char goes in the front half.
38 # e.g. 'abcde', the front half is 'abc', the back half 'de'.
39 # Given 2 strings, a and b, return a string of the form
40 #  a-front + b-front + a-back + b-back
41 def front_back(a,b):
42     def front(s):  #python支持函数嵌套
43         if (len(s)/2)*2 == len(s):#s的长度为偶数,也可以通过求余%实现
44             return s[0:len(s)/2]
45         else:
46             return s[0:len(s)/2+1]
47     def back(s):
48         if (len(s)/2)*2 == len(s):
49             return s[len(s)/2:]
50         else:
51             return s[len(s)/2+1:]
52     return front(a)+front(b)+back(a)+back(b)
53 
54 # Simple provided test() function used in main() to print
55 # what each function returns vs. what it's supposed to return.
56 def test(got, expected):
57     if got == expected:
58         prefix = ' OK '
59     else:
60         prefix = '  X '
61     print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
62 
63 # main() calls the above functions with interesting inputs,
64 # using the above test() to check if the result is correct or not.
65 def main():
66     print 'verbing'
67     test(verbing('hail'), 'hailing')
68     test(verbing('swiming'), 'swimingly')
69     test(verbing('do'), 'do')
70     print
71     print 'not_bad'
72     test(not_bad('This movie is not so bad'), 'This movie is good')
73     test(not_bad('This dinner is not that bad!'), 'This dinner is good!')
74     test(not_bad('This tea is not hot'), 'This tea is not hot')
75     test(not_bad("It's bad yet not"), "It's bad yet not")
76     test(not_bad("It's bad yet not and bad"), "It's bad yet not and bad")
77     print
78     print 'front_back'
79     test(front_back('abcd', 'xy'), 'abxcdy')
80     test(front_back('abcde', 'xyz'), 'abcxydez')
81     test(front_back('Kitten', 'Donut'), 'KitDontenut')
82 if __name__ == '__main__':
83     main()

运行结果:

 

verbing
 OK  got: 'hailing' expected: 'hailing'
 OK  got: 'swimingly' expected: 'swimingly'
 OK  got: 'do' expected: 'do'

not_bad
 OK  got: 'This movie is good' expected: 'This movie is good'
 OK  got: 'This dinner is good!' expected: 'This dinner is good!'
 OK  got: 'This tea is not hot' expected: 'This tea is not hot'
 OK  got: "It's bad yet not" expected: "It's bad yet not"
 OK  got: "It's bad yet not and bad" expected: "It's bad yet not and bad"

front_back
 OK  got: 'abxcdy' expected: 'abxcdy'
 OK  got: 'abcxydez' expected: 'abcxydez'
 OK  got: 'KitDontenut' expected: 'KitDontenut'

 

ps:

关于E项not_bad()函数:

  1. test(not_bad("It's bad yet not and bad"), "It's bad yet not and bad") 是我自己添加的测试项,检测not bad均为首次出现这一边界条件。
  2. 很容易想到的是利用正则表达式表达如下:
    1 def not_bad(s):
    2     import re
    3     m = re.compile('not.*bad').search(s)
    4     if m:
    5         s = s[0:m.start()]+'good'+s[m.end():]
    6     return s

    正则表达式匹配原理类似于拿一个'not.*bad'的滑动窗口与串s进行匹配(注意不匹配换行符\n),所以对ps 1中的test测试结项是能找到匹配项的,运行结果如下:  

not_bad
 OK  got: 'This movie is good' expected: 'This movie is good'
 OK  got: 'This dinner is good!' expected: 'This dinner is good!'
 OK  got: 'This tea is not hot' expected: 'This tea is not hot'
 OK  got: "It's bad yet not" expected: "It's bad yet not"
  X  got: "It's bad yet good!" expected: "It's bad yet not and bad!"

 

posted @ 2013-03-30 12:12  Emma437211  阅读(267)  评论(0编辑  收藏  举报