string1.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3 -tt
  2. # Copyright 2010 Google Inc.
  3. # Licensed under the Apache License, Version 2.0
  4. # http://www.apache.org/licenses/LICENSE-2.0
  5. # Google's Python Class
  6. # http://code.google.com/edu/languages/google-python-class/
  7. # Basic string exercises
  8. # Fill in the code for the functions below. main() is already set up
  9. # to call the functions with a few different inputs,
  10. # printing 'OK' when each function is correct.
  11. # The starter code for each function includes a 'return'
  12. # which is just a placeholder for your code.
  13. # It's ok if you do not complete all the functions, and there
  14. # are some additional functions to try in string2.py.
  15. # A. donuts
  16. # Given an int count of a number of donuts, return a string
  17. # of the form 'Number of donuts: <count>', where <count> is the number
  18. # passed in. However, if the count is 10 or more, then use the word 'many'
  19. # instead of the actual count.
  20. # So donuts(5) returns 'Number of donuts: 5'
  21. # and donuts(23) returns 'Number of donuts: many'
  22. def donuts(count: int):
  23. if count >= 10:
  24. return str('Number of donuts: many')
  25. else:
  26. return str('Number of donuts: %s' % count)
  27. # B. both_ends
  28. # Given a string s, return a string made of the first 2
  29. # and the last 2 chars of the original string,
  30. # so 'spring' yields 'spng'. However, if the string length
  31. # is less than 2, return instead the empty string.
  32. def both_ends(s: str):
  33. if len(s) < 2:
  34. return str()
  35. else:
  36. return s[:2]+s[-2:]
  37. # C. fix_start
  38. # Given a string s, return a string
  39. # where all occurences of its first char have
  40. # been changed to '*', except do not change
  41. # the first char itself.
  42. # e.g. 'babble' yields 'ba**le'
  43. # Assume that the string is length 1 or more.
  44. # Hint: s.replace(stra, strb) returns a version of string s
  45. # where all instances of stra have been replaced by strb.
  46. def fix_start(s: str):
  47. if len(s) > 1:
  48. return s[0]+s[1:].replace(s[0], '*')
  49. # D. MixUp
  50. # Given strings a and b, return a single string with a and b separated
  51. # by a space '<a> <b>', except swap the first 2 chars of each string.
  52. # e.g.
  53. # 'mix', pod' -> 'pox mid'
  54. # 'dog', 'dinner' -> 'dig donner'
  55. # Assume a and b are length 2 or more.
  56. def mix_up(a: str, b: str):
  57. if len(a) > 1 and len(b) > 1:
  58. return a.replace(a[:2], b[:2], 1)+' '+b.replace(b[:2], a[:2], 1)
  59. # Provided simple test() function used in main() to print
  60. # what each function returns vs. what it's supposed to return.
  61. def test(got, expected):
  62. if got == expected:
  63. prefix = ' OK '
  64. else:
  65. prefix = ' X '
  66. print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
  67. # Provided main() calls the above functions with interesting inputs,
  68. # using test() to check if each result is correct or not.
  69. def main():
  70. print('donuts')
  71. # Each line calls donuts, compares its result to the expected for that call.
  72. test(donuts(4), 'Number of donuts: 4')
  73. test(donuts(9), 'Number of donuts: 9')
  74. test(donuts(10), 'Number of donuts: many')
  75. test(donuts(99), 'Number of donuts: many')
  76. print
  77. print('both_ends')
  78. test(both_ends('spring'), 'spng')
  79. test(both_ends('Hello'), 'Helo')
  80. test(both_ends('a'), '')
  81. test(both_ends('xyz'), 'xyyz')
  82. print
  83. print('fix_start')
  84. test(fix_start('babble'), 'ba**le')
  85. test(fix_start('aardvark'), 'a*rdv*rk')
  86. test(fix_start('google'), 'goo*le')
  87. test(fix_start('donut'), 'donut')
  88. print
  89. print('mix_up')
  90. test(mix_up('mix', 'pod'), 'pox mid')
  91. test(mix_up('dog', 'dinner'), 'dig donner')
  92. test(mix_up('gnash', 'sport'), 'spash gnort')
  93. test(mix_up('pezzy', 'firm'), 'fizzy perm')
  94. # Standard boilerplate to call the main() function.
  95. if __name__ == '__main__':
  96. main()