파이썬(python) 스터디 – String
Programming Languages 에서 Google’s Python Class 의 String 을 스터디.. http://code.google.com/intl/ko-KR/edu/languages/google-python-class/strings.html 대충 읽고 나니 연습문제가! 지난번에 받은 google-python-exercises 에서 basic/string1.py 을 풀어보았다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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): result = 'Number of donuts: '; if count >= 10: result = result + 'many' else: result = result + str(count) return result # 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: result = s[0:2] + s[-2:] else: result = '' return result # 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): result = s.replace(s[0], '*') return s[0] + result[1:] # 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): left = b[0:2] + a[2:] right = a[0:2] + b[2:] return left + " " + right |
결과 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
basic apollo89$ python string1.py 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' basic apollo89$ |
내친김에 basic/string2.py 까지 고고~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# D. verbing # Given a string, if its length is at least 3, # add 'ing' to its end. # Unless it already ends in 'ing', in which case # add 'ly' instead. # If the string length is less than 3, leave it unchanged. # Return the resulting string. def verbing(s): if len(s) < 3: result = s else : if s[-3:] == 'ing': result = s + 'ly' if s[-3:] != 'ing': result = s + 'ing' return result # E. not_bad # Given a string, find the first appearance of the # substring 'not' and 'bad'. If the 'bad' follows # the 'not', replace the whole 'not'...'bad' substring # with 'good'. # Return the resulting string. # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): findNot = s.find('not') findBad = s.find('bad') if findBad > findNot: result = s[:findNot] + 'good' + s[findBad+3:] else: result = s return result # F. front_back # Consider dividing a string into two halves. # If the length is even, the front and back halves are the same length. # If the length is odd, we'll say that the extra char goes in the front half. # e.g. 'abcde', the front half is 'abc', the back half 'de'. # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): if len(a)%2 == 1: aFront = a[0:(len(a)/2)+1] aBack = a[(len(a)/2)+1:] else: aFront = a[0:(len(a)/2)] aBack = a[(len(a)/2):] if len(b)%2 == 1: bFront = b[0:(len(b)/2)+1] bBack = b[(len(b)/2)+1:] else: bFront = b[0:(len(b)/2)] bBack = b[(len(b)/2):] return aFront + bFront + aBack + bBack |
결과 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
basic apollo89$ python string2.py 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" front_back OK got: 'abxcdy' expected: 'abxcdy' OK got: 'abcxydez' expected: 'abcxydez' OK got: 'KitDontenut' expected: 'KitDontenut' basic apollo89$ |