Advertisement
proffreda

lab06.py

Oct 13th, 2016
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. ## Lab 6: Trees ##
  2.  
  3. # pyTunes
  4. def make_pytunes(username):
  5. """Return a pyTunes tree as shown in the diagram with USERNAME as the value
  6. of the root.
  7.  
  8. >>> pytunes = make_pytunes('i_love_music')
  9. >>> print_tree(pytunes)
  10. i_love_music
  11. pop
  12. justin bieber
  13. single
  14. what do you mean?
  15. 2015 pop mashup
  16. trance
  17. darude
  18. sandstorm
  19. """
  20. "*** YOUR CODE HERE ***"
  21.  
  22. def num_songs(t):
  23. """Return the number of songs in the pyTunes tree, t.
  24.  
  25. >>> pytunes = make_pytunes('i_love_music')
  26. >>> num_songs(pytunes)
  27. 3
  28. """
  29. "*** YOUR CODE HERE ***"
  30.  
  31. def add_song(t, song, category):
  32. """Returns a new tree with SONG added to CATEGORY. Assume the CATEGORY
  33. already exists.
  34.  
  35. >>> indie_tunes = tree('indie_tunes',
  36. ... [tree('indie',
  37. ... [tree('vance joy',
  38. ... [tree('riptide')])])])
  39. >>> new_indie = add_song(indie_tunes, 'georgia', 'vance joy')
  40. >>> print_tree(new_indie)
  41. indie_tunes
  42. indie
  43. vance joy
  44. riptide
  45. georgia
  46.  
  47. """
  48. "*** YOUR CODE HERE ***"
  49.  
  50. # Tree ADT
  51. def tree(root, branches=[]):
  52. for branch in branches:
  53. assert is_tree(branch), 'branches must be trees'
  54. return [root] + list(branches)
  55.  
  56. def root(tree):
  57. return tree[0]
  58.  
  59. def branches(tree):
  60. return tree[1:]
  61.  
  62. def is_tree(tree):
  63. if type(tree) != list or len(tree) < 1:
  64. return False
  65. for branch in branches(tree):
  66. if not is_tree(branch):
  67. return False
  68. return True
  69.  
  70. def is_leaf(tree):
  71. return not branches(tree)
  72.  
  73. def print_tree(t, indent=0):
  74. """Print a representation of this tree in which each node is
  75. indented by two spaces times its depth from the entry.
  76.  
  77. >>> print_tree(tree(1))
  78. 1
  79. >>> print_tree(tree(1, [tree(2)]))
  80. 1
  81. 2
  82. >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
  83. >>> print_tree(numbers)
  84. 1
  85. 2
  86. 3
  87. 4
  88. 5
  89. 6
  90. 7
  91. """
  92. print(' ' * indent + str(root(t)))
  93. for b in branches(t):
  94. print_tree(b, indent + 1)
  95.  
  96. def copy_tree(t):
  97. """Returns a copy of t. Only for testing purposes.
  98.  
  99. >>> t = tree(5)
  100. >>> copy = copy_tree(t)
  101. >>> t = tree(6)
  102. >>> print_tree(copy)
  103. 5
  104. """
  105. return tree(root(t), [copy_tree(b) for b in branches(t)])
  106.  
  107.  
  108. def delete(t, target):
  109. """Returns the tree that results from deleting TARGET from t. If TARGET is
  110. a category, delete everything inside of it.
  111.  
  112. >>> my_account = tree('kpop_king',
  113. ... [tree('korean',
  114. ... [tree('gangnam style'),
  115. ... tree('wedding dress')]),
  116. ... tree('pop',
  117. ... [tree('t-swift',
  118. ... [tree('blank space')]),
  119. ... tree('uptown funk'),
  120. ... tree('see you again')])])
  121. >>> new = delete(my_account, 'pop')
  122. >>> print_tree(new)
  123. kpop_king
  124. korean
  125. gangnam style
  126. wedding dress
  127. """
  128. "*** YOUR CODE HERE ***"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement