balderman

Untitled

Aug 3rd, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. """ This program has been adapted for use by GVAHIM
  2. - the main revisions regard pep8 compliance and use of variable names
  3.  
  4. Copyright 2010 Google Inc.
  5. Licensed under the Apache License, Version 2.0
  6. http://www.apache.org/licenses/LICENSE-2.0
  7.  
  8. Google's Python Class
  9. http://code.google.com/edu/languages/google-python-class/
  10.  
  11. Basic list exercises
  12. Fill in the code for the functions below. main() is already set up
  13. to call the functions with a few different inputs,
  14. printing 'OK' when each function is correct.
  15. The starter code for each function includes a 'return'
  16. which is just a placeholder for your code.
  17. It's ok if you do not complete all the functions, and there
  18. are some additional functions to try in list2.py. """
  19.  
  20.  
  21. # A. match_ends
  22. # Given a list of strings, return the count of the number of
  23. # strings where the string length is 2 or more and the first
  24. # and last chars of the string are the same.
  25. # Note: python does not have a ++ operator, but += works.
  26. def match_ends(words):
  27. """ +++your code here+++ """
  28. match_counter = 0
  29. for word in words:
  30. if len(word) >= 2 and word[0] == word[-1]:
  31. match_counter += 1
  32. return match_counter
  33.  
  34.  
  35. # B. front_x
  36. # Given a list of strings, return a list with the strings
  37. # in sorted order, except group all the strings that begin with 'x' first.
  38. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
  39. # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  40. # Hint: this can be done by making 2 lists and sorting each of them
  41. # before combining them.
  42. def front_x(words):
  43. """ +++your code here+++ """
  44. x_words = []
  45. non_x_words = []
  46. for word in words:
  47. if word[0] == 'x':
  48. x_words.append(word)
  49. else:
  50. non_x_words.append(word)
  51. x_words = sorted(x_words)
  52. non_x_words = sorted(non_x_words)
  53. return x_words + non_x_words
  54.  
  55.  
  56. # C. sort_last
  57. # Given a list of non-empty tuples, return a list sorted in increasing
  58. # order by the last element in each tuple.
  59. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
  60. # [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
  61. # Hint: use a custom key= function to extract the last element form each tuple.
  62. def sort_last(tuples):
  63. """ +++your code here+++ """
  64. return sorted(tuples, key=lambda x: x[-1])
  65.  
  66.  
  67. # D. Given a list of numbers, return a list where
  68. # all adjacent == elements have been reduced to a single element,
  69. # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
  70. # modify the passed in list.
  71. def remove_adjacent(nums):
  72. """ +++your code here+++ """
  73. result = [nums[0]] if nums else []
  74. for idx, num in enumerate(nums):
  75. if idx > 0:
  76. if num != nums[idx - 1]:
  77. result.append(num)
  78. return result
  79.  
  80.  
  81. # E. Given two lists sorted in increasing order, create and return a merged
  82. # list of all the elements in sorted order. You may modify the passed in lists.
  83. # Ideally, the solution should work in "linear" time, making a single
  84. # pass of both lists.
  85. #
  86. # NOTE - DO NOT use return sorted(sorted1 + sorted2) - that's too easy :-)
  87. #
  88. def linear_merge(sorted1, sorted2):
  89. """ +++your code here+++ """
  90. size_1 = len(sorted1)
  91. size_2 = len(sorted2)
  92.  
  93. res = []
  94. i, j = 0, 0
  95.  
  96. while i < size_1 and j < size_2:
  97. if sorted1[i] < sorted2[j]:
  98. res.append(sorted1[i])
  99. i += 1
  100.  
  101. else:
  102. res.append(sorted2[j])
  103. j += 1
  104.  
  105. res = res + sorted1[i:] + sorted2[j:]
  106. return res
  107.  
  108.  
  109. def test(got, expected):
  110. """ simple test() function used in main() to print
  111. what each function returns vs. what it's supposed to return. """
  112.  
  113. if got == expected:
  114. prefix = ' OK '
  115. else:
  116. prefix = ' X '
  117. print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
  118.  
  119.  
  120. def main():
  121. """ main() calls the above functions with interesting inputs,
  122. using test() to check if each result is correct or not. """
  123.  
  124. print('\nmatch_ends')
  125. test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  126. test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
  127. test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
  128.  
  129. print('\nfront_x')
  130. test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
  131. ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
  132. test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
  133. ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
  134. test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
  135. ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
  136.  
  137. print('\nsort_last')
  138. test(sort_last([(1, 3), (3, 2), (2, 1)]),
  139. [(2, 1), (3, 2), (1, 3)])
  140. test(sort_last([(2, 3), (1, 2), (3, 1)]),
  141. [(3, 1), (1, 2), (2, 3)])
  142. test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
  143. [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
  144.  
  145. print('\nremove_adjacent')
  146. test(remove_adjacent([1, 2, 2, 3]), [1, 2, 3])
  147. test(remove_adjacent([2, 2, 3, 3, 3]), [2, 3])
  148. test(remove_adjacent([]), [])
  149.  
  150. print('\nlinear_merge')
  151. test(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
  152. ['aa', 'bb', 'cc', 'xx', 'zz'])
  153. test(linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']),
  154. ['aa', 'bb', 'cc', 'xx', 'zz'])
  155. test(linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']),
  156. ['aa', 'aa', 'aa', 'bb', 'bb'])
  157.  
  158.  
  159. if __name__ == '__main__':
  160. main()
  161.  
  162.  
  163. ------------
  164. output
  165. ------------
  166. match_ends
  167. OK got: 3 expected: 3
  168. OK got: 2 expected: 2
  169. OK got: 1 expected: 1
  170.  
  171. front_x
  172. OK got: ['xaa', 'xzz', 'axx', 'bbb', 'ccc'] expected: ['xaa', 'xzz', 'axx', 'bbb', 'ccc']
  173. OK got: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'] expected: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc']
  174. OK got: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] expected: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  175.  
  176. sort_last
  177. OK got: [(2, 1), (3, 2), (1, 3)] expected: [(2, 1), (3, 2), (1, 3)]
  178. OK got: [(3, 1), (1, 2), (2, 3)] expected: [(3, 1), (1, 2), (2, 3)]
  179. OK got: [(2, 2), (1, 3), (3, 4, 5), (1, 7)] expected: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
  180.  
  181. remove_adjacent
  182. OK got: [1, 2, 3] expected: [1, 2, 3]
  183. OK got: [2, 3] expected: [2, 3]
  184. OK got: [] expected: []
  185.  
  186. linear_merge
  187. OK got: ['aa', 'bb', 'cc', 'xx', 'zz'] expected: ['aa', 'bb', 'cc', 'xx', 'zz']
  188. OK got: ['aa', 'bb', 'cc', 'xx', 'zz'] expected: ['aa', 'bb', 'cc', 'xx', 'zz']
  189. OK got: ['aa', 'aa', 'aa', 'bb', 'bb'] expected: ['aa', 'aa', 'aa', 'bb', 'bb']
  190.  
Add Comment
Please, Sign In to add comment