This repository was archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcc9514
commit e32d77f
Showing
7 changed files
with
384 additions
and
23 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
Coursework for self-studying UC Berkeley's CS61A (Spring 2018). | ||
Coursework for self-studying UC Berkeley's CS61A in Spring 2018 (WIP). | ||
|
||
Link to course website: https://inst.eecs.berkeley.edu/~cs61a/sp18/ | ||
Link to course website: https://inst.eecs.berkeley.edu/~cs61a/sp18/ | ||
|
||
## Completion Matrix | ||
|
||
| Labs | Homework | Discussions** | Exam Preps | CS Mentors | Projects* | | ||
| ------------------------ | ----------------------- | ------------------------- | ---------- | ---------- | ------------------------- | | ||
| lab01 :heavy_check_mark: | hw01 :heavy_check_mark: | disc01 :heavy_check_mark: | prep01 :x: | csm01 :x: | hog :heavy_check_mark: | | ||
| lab02 :heavy_check_mark: | hw02 :heavy_check_mark: | disc02 :heavy_check_mark: | prep02 :x: | csm02 :x: | maps :heavy_check_mark: | | ||
| lab03 :heavy_check_mark: | hw03 :heavy_check_mark: | disc03 :heavy_check_mark: | prep03 :x: | csm03 :x: | ants :heavy_check_mark: | | ||
| lab04 :heavy_check_mark: | hw04 :heavy_check_mark: | disc04 :x: | prep04 :x: | csm04 :x: | scheme :heavy_check_mark: | | ||
| lab05 :heavy_check_mark: | hw05 :heavy_check_mark: | disc05 :x: | prep05 :x: | csm05 :x: | | | ||
| lab06 :heavy_check_mark: | hw06 :heavy_check_mark: | disc06 :x: | prep06 :x: | csm06 :x: | | | ||
| lab07 :heavy_check_mark: | hw07 :heavy_check_mark: | disc07 :x: | prep07 :x: | csm07 :x: | | | ||
| lab08 :heavy_check_mark: | hw08 :heavy_check_mark: | disc08 :x: | prep08 :x: | csm08 :x: | | | ||
| lab09 :heavy_check_mark: | hw09 :heavy_check_mark: | disc09 :x: | prep09 :x: | csm09 :x: | | | ||
| lab10 :heavy_check_mark: | hw10 :x: | disc10 :x: | prep10 :x: | | | | ||
| lab11 :x: | hw11 :x: | disc11 :x: | | | | | ||
| lab12 :x: | hw12 :x: | | | | | | ||
| lab13 :x: | hw13 :x: | | | | | | ||
|
||
\*The **Hog contest** and **Scheme Art** projects were not included since they require online interaction within the class. | ||
|
||
\*\*Some of the non-code questions in the discussions such as drawing environment diagrams are omitted for obvious reasons. |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
def tree_max(t): | ||
"""Return the max of a tree | ||
>>> t = tree(1, | ||
... [tree(3, | ||
... [tree(4), | ||
... tree(5), | ||
... tree(6)]), | ||
... tree(2)]) | ||
>>> tree_max(t) | ||
6 | ||
>>> s = tree(11, | ||
... [tree(32, | ||
... [tree(4), | ||
... tree(15, | ||
... [tree(21), | ||
... tree(45)]), | ||
... tree(8)]), | ||
... tree(2, | ||
... [tree(12), | ||
... tree(2)])]) | ||
>>> tree_max(s) | ||
45 | ||
""" | ||
"""BEGIN PROBLEM 3.1""" | ||
# strat: simply collect all the labels recursively and get the max value from the branches and leaves | ||
return max([label(t)] + [tree_max(b) for b in branches(t)]) | ||
"""END PROBLEM 3.1""" | ||
|
||
def height(t): | ||
"""Return the height of a tree | ||
>>> t = tree(1, | ||
... [tree(3, | ||
... [tree(4), | ||
... tree(5), | ||
... tree(6)]), | ||
... tree(2)]) | ||
>>> height(t) | ||
2 | ||
>>> s = tree(11, | ||
... [tree(32, | ||
... [tree(4), | ||
... tree(15, | ||
... [tree(21), | ||
... tree(45)]), | ||
... tree(8)]), | ||
... tree(2, | ||
... [tree(12), | ||
... tree(2)])]) | ||
>>> height(s) | ||
3 | ||
""" | ||
"""BEGIN PROBLEM 3.2""" | ||
# strat: | ||
# - recursively walk the branches and keep track of each depth and we go in one layer at a time | ||
# - for each branch, take the max value after calling height() for each branch and add 1 before returning it | ||
# - if we hit a leaf, then there's no more branches to go through...just return 0 | ||
return 0 if is_leaf(t) else (1 + max([height(b) for b in branches(t)])) | ||
"""END PROBLEM 3.2""" | ||
|
||
def square_tree(t): | ||
"""Return a tree with the square of every element in 't'. It shouldn't modify the original tree | ||
>>> t = tree(1, | ||
... [tree(3, | ||
... [tree(4), | ||
... tree(5), | ||
... tree(6)]), | ||
... tree(2)]) | ||
>>> print_tree(square_tree(t)) | ||
1 | ||
9 | ||
16 | ||
25 | ||
36 | ||
4 | ||
>>> print_tree(t) | ||
1 | ||
3 | ||
4 | ||
5 | ||
6 | ||
2 | ||
""" | ||
"""BEGIN PROBLEM 3.3""" | ||
# no need to make an explicit copy (via splicing) of the tree when you can reconstruct one from the constructor directly | ||
return tree(label(t) ** 2, [square_tree(b) for b in branches(t)]) | ||
"""END PROBLEM 3.3""" | ||
|
||
|
||
def find_path(tree, x): | ||
"""Return a list showing the branch values for getting to a node labeled 'x'. | ||
Extra challenge (for me): if x is detected in tree while parsing branches, return the path immediately | ||
>>> 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 | ||
>>> find_path(t, 6) | ||
[2, 7, 6] | ||
""" | ||
"""BEGIN PROBLEM 3.4""" | ||
# strat: | ||
# - walk through the branches until we find the leaves or we find a label that matches 'x' | ||
# - if 'x' exists in the original tree, then return it inside an array and add the connecting branches along the way | ||
# - otherwise, return None if 'x' doesn't exist in the tree | ||
if label(tree) == x: | ||
return [x] # we found 'x', return it inside an array | ||
for path in [find_path(branch, x) for branch in branches(tree)]: | ||
if path: # if this is a truthy value, then attach the branch's label in front of the path value and return it | ||
return [label(tree)] + path | ||
"""END PROBLEM 3.4""" | ||
|
||
def prune(t, k): | ||
"""A function that takes in a tree and a depth 'k' and returns a new | ||
tree that contains only the first 'k' levels of the original tree. | ||
>>> t = tree(2, | ||
... [tree(7, | ||
... [tree(3), | ||
... tree(6, | ||
... [tree(5), | ||
... tree(11)])]), | ||
... tree(15)]) | ||
>>> print_tree(prune(t, 2)) | ||
2 | ||
7 | ||
3 | ||
6 | ||
15 | ||
""" | ||
"""BEGIN PROBLEM 3.5""" | ||
if k == 0: | ||
return tree(label(t)) | ||
return tree(label(t), [prune(b, k - 1) for b in branches(t)]) | ||
"""END PROBLEM 3.5""" | ||
|
||
# Tree-related functions | ||
|
||
# Constructor | ||
def tree(label, branches=[]): | ||
for branch in branches: | ||
assert is_tree(branch) | ||
return [label] + list(branches) | ||
|
||
# Selectors | ||
def label(tree): | ||
return tree[0] | ||
|
||
def branches(tree): | ||
return tree[1:] | ||
|
||
# For convenience | ||
def is_tree(tree): | ||
if type(tree) != list or len(tree) < 1: | ||
return False | ||
for branch in branches(tree): | ||
if not is_tree(branch): | ||
return False | ||
return True | ||
|
||
def is_leaf(tree): | ||
return not branches(tree) | ||
|
||
def print_tree(t, indent=0): | ||
"""Print a representation of this tree in which each node is | ||
indented by two spaces times its depth from the root.""" | ||
print(' ' * indent + str(label(t))) | ||
[print_tree(branch, indent + 1) for branch in branches(t)] | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod(exclude_empty=True) |
Oops, something went wrong.