Created
July 10, 2020 19:08
-
-
Save jemmy-z/6c9510815859daf817a9f805a04e0ecb to your computer and use it in GitHub Desktop.
Code for Discussion 5 CS 61A Summer 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def square_tree(t): | |
"""Return a tree with the square of every element in t | |
>>> numbers = tree(1, | |
... [tree(2, | |
... [tree(3), | |
... tree(4)]), | |
... tree(5, | |
... [tree(6, | |
... [tree(7)]), | |
... tree(8)])]) | |
>>> print_tree(square_tree(numbers)) | |
1 | |
4 | |
9 | |
16 | |
25 | |
36 | |
49 | |
64 | |
""" | |
# YOUR CODE HERE | |
def find_path(tree, x): | |
""" | |
>>> t = tree(2, [tree(7, [tree(3), tree(6, [tree(5), tree(11)])] ), tree(15)]) | |
>>> find_path(t, 5) | |
[2, 7, 6, 5] | |
>>> find_path(t, 10) # returns None | |
""" | |
if _____________________________: | |
return _____________________________ | |
_____________________________: | |
path = _____________________________ | |
if _____________________________: | |
return _____________________________ | |
def add_this_many(x, el, lst): | |
""" Adds el to the end of lst the number of times x occurs | |
in lst. | |
>>> lst = [1, 2, 4, 2, 1] | |
>>> add_this_many(1, 5, lst) | |
>>> lst | |
[1, 2, 4, 2, 1, 5, 5] | |
>>> add_this_many(2, 2, lst) | |
>>> lst | |
[1, 2, 4, 2, 1, 5, 5, 2, 2] | |
""" | |
# YOUR CODE HERE | |
def group_by(s, fn): | |
""" | |
>>> group_by([12, 23, 14, 45], lambda p: p // 10) | |
{1: [12, 14], 2: [23], 4: [45]} | |
>>> group_by(range(-3, 4), lambda x: x * x) | |
{0: [0], 1: [-1, 1], 4: [-2, 2], 9: [-3, 3]} | |
""" | |
# YOUR CODE HERE | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment