proffreda

hw03.py

Oct 16th, 2016
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. def is_sorted(lst):
  2. """Returns True if the linked list is sorted.
  3.  
  4. >>> lst1 = link(1, link(2, link(3, link(4))))
  5. >>> is_sorted(lst1)
  6. True
  7. >>> lst2 = link(1, link(3, link(2, link(4, link(5)))))
  8. >>> is_sorted(lst2)
  9. False
  10. >>> lst3 = link(3, link(3, link(3)))
  11. >>> is_sorted(lst3)
  12. True
  13. """
  14. "*** YOUR CODE HERE ***"
  15.  
  16.  
  17. def interleave(s0, s1):
  18. """Interleave linked lists s0 and s1 to produce a new linked
  19. list.
  20.  
  21. >>> evens = link(2, link(4, link(6, link(8, empty))))
  22. >>> odds = link(1, link(3, empty))
  23. >>> print_link(interleave(odds, evens))
  24. 1 2 3 4 6 8
  25. >>> print_link(interleave(evens, odds))
  26. 2 1 4 3 6 8
  27. >>> print_link(interleave(odds, odds))
  28. 1 1 3 3
  29. """
  30. "*** YOUR CODE HERE ***"
  31.  
  32.  
  33. def height(t):
  34. """Return the depth of the deepest node in the tree.
  35.  
  36. >>> height(tree(1))
  37. 0
  38. >>> height(tree(1, [tree(2), tree(3)]))
  39. 1
  40. >>> print_tree(numbers)
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. >>> height(numbers)
  49. 2
  50. """
  51. "*** YOUR CODE HERE ***"
  52.  
  53.  
  54. def sprout_leaves(t, vals):
  55. """Sprout new leaves containing the data in vals at each leaf in
  56. the original tree t and return the resulting tree.
  57.  
  58. >>> t1 = tree(1, [tree(2), tree(3)])
  59. >>> print_tree(t1)
  60. 1
  61. 2
  62. 3
  63. >>> new1 = sprout_leaves(t1, [4, 5])
  64. >>> print_tree(new1)
  65. 1
  66. 2
  67. 4
  68. 5
  69. 3
  70. 4
  71. 5
  72.  
  73. >>> t2 = tree(1, [tree(2, [tree(3)])])
  74. >>> print_tree(t2)
  75. 1
  76. 2
  77. 3
  78. >>> new2 = sprout_leaves(t2, [6, 1, 2])
  79. >>> print_tree(new2)
  80. 1
  81. 2
  82. 3
  83. 6
  84. 1
  85. 2
  86. """
  87. "*** YOUR CODE HERE ***"
  88.  
  89.  
  90. ###################
  91. # Linked List ADT #
  92. ###################
  93.  
  94. # Linked List definition
  95. empty = 'empty'
  96.  
  97. def is_link(s):
  98. """s is a linked list if it is empty or a (first, rest) pair."""
  99. return s == empty or (type(s) == list and len(s) == 2 and is_link(s[1]))
  100.  
  101. def link(first, rest=empty):
  102. """Construct a linked list from its first element and the rest."""
  103. assert is_link(rest), 'rest must be a linked list.'
  104. return [first, rest]
  105.  
  106. def first(s):
  107. """Return the first element of a linked list s."""
  108. assert is_link(s), 'first only applies to linked lists.'
  109. assert s != empty, 'empty linked list has no first element.'
  110. return s[0]
  111.  
  112. def rest(s):
  113. """Return the rest of the elements of a linked list s."""
  114. assert is_link(s), 'rest only applies to linked lists.'
  115. assert s != empty, 'empty linked list has no rest.'
  116. return s[1]
  117.  
  118. def print_link(s):
  119. """Print elements of a linked list s.
  120.  
  121. >>> s = link(1, link(2, link(3, empty)))
  122. >>> print_link(s)
  123. 1 2 3
  124. """
  125. line = ''
  126. while s != empty:
  127. if line:
  128. line += ' '
  129. line += str(first(s))
  130. s = rest(s)
  131. print(line)
  132.  
  133.  
  134. ############
  135. # Tree ADT #
  136. ############
  137.  
  138. def tree(root, branches=[]):
  139. for branch in branches:
  140. assert is_tree(branch), 'branches must be trees'
  141. return [root] + list(branches)
  142.  
  143. def root(tree):
  144. return tree[0]
  145.  
  146. def branches(tree):
  147. return tree[1:]
  148.  
  149. def is_tree(tree):
  150. if type(tree) != list or len(tree) < 1:
  151. return False
  152. for branch in branches(tree):
  153. if not is_tree(branch):
  154. return False
  155. return True
  156.  
  157. def is_leaf(tree):
  158. return not branches(tree)
  159.  
  160. def print_tree(t, indent=0):
  161. """Print a representation of this tree in which each node is
  162. indented by two spaces times its depth from the entry.
  163.  
  164. >>> print_tree(tree(1))
  165. 1
  166. >>> print_tree(tree(1, [tree(2)]))
  167. 1
  168. 2
  169. >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
  170. >>> print_tree(numbers)
  171. 1
  172. 2
  173. 3
  174. 4
  175. 5
  176. 6
  177. 7
  178. """
  179. print(' ' * indent + str(root(t)))
  180. for b in branches(t):
  181. print_tree(b, indent + 1)
  182.  
  183. numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
Advertisement
Add Comment
Please, Sign In to add comment