Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
added disc03 + completion matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
tejashah88 committed Sep 8, 2018
1 parent fcc9514 commit e32d77f
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 23 deletions.
26 changes: 24 additions & 2 deletions README.md
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.
12 changes: 6 additions & 6 deletions discussions/disc01/disc01.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def wears_jacket(temp, raining):
"""
"""Returns true if the temperature is less than 60 degrees OR it is raining
>>> wears_jacket(90, False)
False
>>> wears_jacket(40, False)
Expand All @@ -12,7 +12,7 @@ def wears_jacket(temp, raining):
"""END PROBLEM 1.1"""

def handle_overflow(s1, s2):
"""
"""Prints the instructions for handling potential overflows between 2 classes
>>> handle_overflow(27, 15)
No overflow
>>> handle_overflow(35, 29)
Expand All @@ -34,7 +34,7 @@ def handle_overflow(s1, s2):
"""END PROBLEM 1.2"""

def is_prime(n):
"""
"""Returns if the given number is a prime number or not
>>> is_prime(10)
False
>>> is_prime(7)
Expand All @@ -44,9 +44,9 @@ def is_prime(n):
if n in [2, 3]: # shortcut for if n equals 2 or 3
return True

if n % 2 == 0 or n < 2: # skip even numbers
if n % 2 == 0 or n < 2: # skip even numbers, since they are not prime anyways
return False
# only iterate the odd numbers from 3 to the nearest whole number of sqrt(n)
# only iterate the odd numbers from 3 to the nearest whole number of sqrt(n), skipping even numbers
for i in range(3, round(n ** 0.5), 2):
if n % i == 0: # if this conditional is true, n isn't prime
return False
Expand Down Expand Up @@ -78,12 +78,12 @@ def keep_ints_compose(n):
"""
"""BEGIN PROBLEM 2.3"""
lst = [i for i in range(1, n + 1)]
# I could have used a lambda expression, but since it returns anything it evaluates, it would fail the test cases
def process(cond):
[print(i) for i in lst if cond(i)]
return process
"""END PROBLEM 2.3"""


if __name__ == "__main__":
import doctest
doctest.testmod(exclude_empty=True)
13 changes: 6 additions & 7 deletions discussions/disc02/disc02.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def multiply(m, n):
"""
"""Recursively multiply the number 'm' by 'n' times
>>> multiply(5, 3)
15
"""
Expand All @@ -8,7 +8,7 @@ def multiply(m, n):
"""END PROBLEM 2.1"""

def countdown(n):
"""
"""Recursively print a countdown from n to 1
>>> countdown(3)
3
2
Expand All @@ -21,7 +21,7 @@ def countdown(n):
"""END PROBLEM 2.2"""

def countup(n):
"""
"""Recursively print a countup (or reverse countdown) from 1 to n
>>> countup(3)
1
2
Expand All @@ -34,7 +34,7 @@ def countup(n):
"""END PROBLEM 2.3"""

def sum_digits(n):
"""
"""Recursively calculate the sum of the digits of 'n'
>>> sum_digits(7)
7
>>> sum_digits(30)
Expand All @@ -47,7 +47,7 @@ def sum_digits(n):
"""END PROBLEM 2.4"""

def count_stair_ways(n):
"""
"""Count the numbers of ways to walk up a flight of stairs with 'n' steps while taking a maximum of 2 steps at a time
>>> count_stair_ways(2)
2
>>> count_stair_ways(3)
Expand All @@ -71,7 +71,7 @@ def count_stair_ways(n):
"""END PROBLEM 3.1"""

def count_k(n, k):
"""
"""Generalized version of count_stair_ways, except that the max number of step to take is defined by 'k'
>>> count_k(3, 3) # 3, 2 + 1, 1 + 2, 1 + 1 + 1
4
>>> count_k(4, 4)
Expand All @@ -88,7 +88,6 @@ def count_k(n, k):
else:
return sum([count_k(n - i, k) for i in range(1, k + 1)])


if __name__ == "__main__":
import doctest
doctest.testmod(exclude_empty=True)
2 changes: 0 additions & 2 deletions discussions/disc02/disc02_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,5 @@ def test_problem_3_2(self):
self.assertEqual(count_k(10, 3), 274)
self.assertEqual(count_k(300, 1), 1)



if __name__ == "__main__":
unittest.main()
175 changes: 175 additions & 0 deletions discussions/disc03/disc03.py
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)
Loading

0 comments on commit e32d77f

Please sign in to comment.