Skip to content

Commit

Permalink
Wed Sep 25 08:48:34 PDT 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Sep 25, 2024
1 parent 781cfbe commit 3e256c9
Show file tree
Hide file tree
Showing 100 changed files with 1,323 additions and 7,420 deletions.
2 changes: 1 addition & 1 deletion _internal/targets.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"due": "9/25/24", "link": "hw/hw03/", "name": "HW 03: Higher-Order Functions", "piazza_name": "HW 03", "release": "9/18/24"}, {"due": "9/27/24", "link": "lab/lab04/", "name": "Lab 04: Recursion", "piazza_name": "Lab 04", "release": "9/23/24"}]
[{"due": "9/25/24", "link": "hw/hw03/", "name": "HW 03: Higher-Order Functions", "piazza_name": "HW 03", "release": "9/18/24"}, {"due": "9/27/24", "link": "lab/lab04/", "name": "Lab 04: Recursion", "piazza_name": "Lab 04", "release": "9/23/24"}, {"due": "10/2/24", "link": "hw/hw04/", "name": "HW 04: Recursion", "piazza_name": "HW 04", "release": "9/25/24"}]
Binary file modified assets/slides/08-Recursion_1pp.pdf
Binary file not shown.
Binary file added assets/slides/09-Tree_Recursion_1pp.pdf
Binary file not shown.
98 changes: 98 additions & 0 deletions assets/slides/09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Tree Recursion

# ----------------------- #
# Tracing factorial

from ucb import trace

@trace
def fact(n):
""" computes the factorial of n (n!)
>>> fact(5) # 5! = 5 * 4 * 3 * 2 * 1
120
"""
if n == 0 or n == 1:
return 1
else:
return n * fact(n-1)

# ----------------------- #
# Mutual Recursion
# (Counting Unique Prime Factors Recursively)
# https://pythontutor.com/render.html#code=def%20smallest_factor%28n%29%3A%0A%20%20%20%20if%20%28n%252%20%3D%3D%200%29%3A%0A%20%20%20%20%20%20%20%20return%202%0A%20%20%20%20k%20%3D%203%0A%20%20%20%20while%20%28k%20%3C%20n%29%3A%0A%20%20%20%20%20%20%20%20if%20%28n%25k%20%3D%3D%200%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20k%0A%20%20%20%20%20%20%20%20k%20%2B%3D%201%0A%20%20%20%20return%20n%0A%0A%0Adef%20unique_prime_factors%28n%29%3A%0A%20%20%20%20%22%22%22Return%20the%20number%20of%20unique%20prime%20factors%20of%20n.%0A%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%2851%29%20%20%23%203%20*%2017%0A%20%20%20%202%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%2827%29%20%20%20%23%203%20*%203%20*%203%0A%20%20%20%201%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%28120%29%20%23%202%20*%202%20*%202%20*%203%20*%205%0A%20%20%20%203%0A%20%20%20%20%22%22%22%0A%20%20%20%20k%20%3D%20smallest_factor%28n%29%0A%20%20%20%20def%20no_k%28n%29%3A%0A%20%20%20%20%20%20%20%20%22Return%20the%20number%20of%20unique%20prime%20factors%20of%20n%20other%20than%20k.%22%0A%20%20%20%20%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%20%20%20%20elif%20n%20%25%20k%20!%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20unique_prime_factors%28n%29%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20no_k%28n//k%29%0A%20%20%20%20return%201%2Bno_k%28n%29%0A%0Aunique_prime_factors%2860%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false
def smallest_factor(n):
if (n%2 == 0):
return 2
k = 3
while (k < n):
if (n%k == 0):
return k
k += 1
return n


def unique_prime_factors(n):
"""Return the number of unique prime factors of n.
>>> unique_prime_factors(51) # 3 * 17
2
>>> unique_prime_factors(27) # 3 * 3 * 3
1
>>> unique_prime_factors(120) # 2 * 2 * 2 * 3 * 5
3
"""
k = smallest_factor(n)
def no_k(n):
"Return the number of unique prime factors of n other than k."
if n == 1:
return 0
elif n % k != 0:
return unique_prime_factors(n)
else:
return no_k(n//k)
return 1+no_k(n)

unique_prime_factors(120)

# ----------------------- #
# Count Partitions
# https://pythontutor.com/cp/composingprograms.html#code=def%20count_partitions%28n,%20m%29%3A%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20elif%20n%20%3C%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20elif%20m%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20with_m%20%3D%20count_partitions%28n-m,%20m%29%20%0A%20%20%20%20%20%20%20%20without_m%20%3D%20count_partitions%28n,%20m-1%29%0A%20%20%20%20%20%20%20%20return%20with_m%20%2B%20without_m%0A%20%20%20%20%20%20%20%20%0Aresult%20%3D%20count_partitions%285,%203%29%0A%0A%23%201%20%2B%201%20%2B%201%20%2B%201%20%2B%201%20%3D%205%0A%23%201%20%2B%201%20%2B%201%20%2B%202%20%2B%20%20%20%3D%205%0A%23%201%20%2B%202%20%2B%202%20%2B%20%20%20%20%20%20%20%3D%205%0A%23%201%20%2B%201%20%2B%203%20%2B%20%20%20%20%20%20%20%3D%205%0A%23%202%20%2B%203%20%2B%20%20%20%20%20%20%20%20%20%20%20%3D%205&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

def count_partitions(n, m):
if n == 0:
return 1
elif n < 0:
return 0
elif m == 0:
return 0
else:
with_m = count_partitions(n-m, m)
without_m = count_partitions(n, m-1)
return with_m + without_m

result = count_partitions(5, 3)

# 1 + 1 + 1 + 1 + 1 = 5
# 1 + 1 + 1 + 2 + = 5
# 1 + 2 + 2 + = 5
# 1 + 1 + 3 + = 5
# 2 + 3 + = 5

# ----------------------- #

def count_park(n):
"""Count the ways to park cars and motorcycles in n adjacent spots.
>>> count_park(1) # '.' or '%'
2
>>> count_park(2) # '..', '.%', '%.', '%%', or '<>'
5
>>> count_park(4) # some examples: '<><>', '.%%.', '%<>%', '%.<>'
29
"""
if n < 0:
return 0
elif n == 0:
return 1
else:
return count_park(n-2) + count_park(n-1) + count_park(n-1)

Binary file added assets/slides/10-Sequences.pdf
Binary file not shown.
110 changes: 110 additions & 0 deletions assets/slides/10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

# Lists practice

digits = [1, 8, 2, 8]
8 in digits
[1, 8] in digits
1 in digits and 8 in digits

for x in digits:
continue
#print(100 * x)

def f(x):
for y in [x, x + 1, x + 2]:
print('this is y:', y)
if y > 0:
return y
return 1000

def print_negatives(s):
for d in s:
d = -d
print(d)
print(s)
s = [2, 3, 4]
return s

# Range practice

def range_practice():
range(4)
list(range(4))
range(-2, 2)
list(range(-2, 2))
range(2, 100000000) # 9 zeroes slows things down a lot
list(range(2, 100000000))
len(range(2, range(0, 23)[9]))

def fib(n):
"""Return the nth fibonnaci number.
>>> fib(0)
0
>>> fib(2)
1
>>> fib(10)
55
"""
fib_number = 0
what_to_add = 1 # to get to the next fib number
for _ in range(n):
what_to_add, fib_number = fib_number, fib_number + what_to_add
return fib_number

# List comprehension practice

xs = range(-10, 11)
ys = [x*x - 2*x + 1 for x in xs]

xs_where_y_is_below_10 = [x for x in xs if x*x - 2*x + 1 < 10]
xs_where_y_is_below_10 = [xs[i] for i in range(len(xs)) if ys[i] < 10]

# Tree recursion practice

goal = 21

def play(strategy0, strategy1, n=0, who = 0, announce=False):
"Play twenty-one starting at n and return the index of the winner."
strategies = [strategy0, strategy1]
while n < goal:
n = n + strategies[who](n)
if announce:
print('Player', who, 'increases n to', n)
who = 1 - who
return who

def two_strat(n):
"Always choose 2."
return 2

def interactive(n):
"Ask the user."
choice = input('Pick 1, 2, or 3: ')
if choice in ['1', '2', '3']:
return int(choice)
else:
print('Invalid choice!')
return interactive(n)

def best(n, other_strategy):
"""Return an best strategy against other_strategy.
>>> beat_two_strat = lambda n: best(n, two_strat)
>>> winner(20, beat_two_strat, two_strat)
1
>>> winner(19, beat_two_strat, two_strat)
0
>>> winner(15, beat_two_strat, two_strat)
0
>>> winner(0, beat_two_strat, two_strat)
0
"""
plan = lambda future_n: best(future_n, other_strategy)
for choice in range(1, 4):
if play(plan, other_strategy, n + choice, 1) == 0:
return choice
return 1

perfect = lambda n: best(n, perfect)

Binary file added hw/.DS_Store
Binary file not shown.
11 changes: 4 additions & 7 deletions hw/hw04/hw04.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
LAB_SOURCE_FILE=__file__


HW_SOURCE_FILE=__file__


Expand Down Expand Up @@ -35,13 +32,13 @@ def digit_distance(n):
>>> digit_distance(3)
0
>>> digit_distance(777)
>>> digit_distance(777) # 0 + 0
0
>>> digit_distance(314)
>>> digit_distance(314) # 2 + 3
5
>>> digit_distance(31415926535)
>>> digit_distance(31415926535) # 2 + 3 + 3 + 4 + ... + 2
32
>>> digit_distance(3464660003)
>>> digit_distance(3464660003) # 1 + 2 + 2 + 2 + ... + 3
16
>>> from construct_check import check
>>> # ban all loops
Expand Down
Binary file modified hw/hw04/hw04.zip
Binary file not shown.
32 changes: 13 additions & 19 deletions hw/hw04/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ <h2 id="getting-started-videos" class="no-toc">Getting Started Videos</h2>
<blockquote><p>To see these videos, you should be logged into your berkeley.edu email.</p></blockquote>


<iframe width="560" height="315" src="https://youtube.com/embed/videoseries?list=PLx38hZJ5RLZd69lK3GQh_OH1u_xmVdBe5"
frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" aria-label = "hw3-getstarted"
allowfullscreen></iframe><p><a href='https://youtu.be/playlist?list=PLx38hZJ5RLZd69lK3GQh_OH1u_xmVdBe5'> YouTube link </a></p>
<iframe width="560" height="315" src="https://youtube.com/embed/videoseries?list=PLx38hZJ5RLZd3VypdvCJVp_nN_ihhDFXi"
frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" aria-label = "c88c-hw4-getstarted"
allowfullscreen></iframe><p><a href='https://youtu.be/playlist?list=PLx38hZJ5RLZd3VypdvCJVp_nN_ihhDFXi'> YouTube link </a></p>
</div>


Expand All @@ -224,7 +224,7 @@ <h3 id="q1-num-eights">Q1: Num Eights</h3>

<p><strong>Important:</strong>
Use recursion; the tests will fail if you use any assignment statements or loops.
(You can, however, use function definitions if you'd like.)</p>
(You can define new functions, but don't put assignment statements there either.)</p>

<solution>

Expand Down Expand Up @@ -272,32 +272,28 @@ <h3 id="q2-digit-distance">Q2: Digit Distance</h3>
consecutive digits. For example:</p>

<ul>
<li>The digit distance of <code>6</code> is <code>0</code>.</li>
<li>The digit distance of <code>61</code> is <code>5</code>, as the absolute value of <code>6 &#x2d; 1</code> is <code>5</code>.</li>
<li>The digit distance of <code>71253</code> is <code>12</code> (<code>6 + 1 + 3 + 2</code>).</li>
<li>The digit distance of <code>71253</code> is <code>12</code> (<code>abs(7&#x2d;1) + abs(1&#x2d;2) + abs(2&#x2d;5) + abs(5&#x2d;3)</code> = <code>6 + 1 + 3 + 2</code>).</li>
<li>The digit distance of <code>6</code> is <code>0</code> because there are no pairs of consecutive digits.</li>
</ul>

<p>Write a function that determines the digit distance of a given positive integer.
<p>Write a function that determines the digit distance of a positive integer.
You must use recursion or the tests will fail.</p>

<blockquote><p><strong>Hint:</strong> There are multiple valid ways of solving this problem!
If you're stuck, try writing out an iterative solution
first, and then convert your iterative solution into a recursive one.</p></blockquote>

<solution>

<pre><code>def digit_distance(n):
&quot;&quot;&quot;Determines the digit distance of n.

&gt;&gt;&gt; digit_distance(3)
0
&gt;&gt;&gt; digit_distance(777)
&gt;&gt;&gt; digit_distance(777) # 0 + 0
0
&gt;&gt;&gt; digit_distance(314)
&gt;&gt;&gt; digit_distance(314) # 2 + 3
5
&gt;&gt;&gt; digit_distance(31415926535)
&gt;&gt;&gt; digit_distance(31415926535) # 2 + 3 + 3 + 4 + ... + 2
32
&gt;&gt;&gt; digit_distance(3464660003)
&gt;&gt;&gt; digit_distance(3464660003) # 1 + 2 + 2 + 2 + ... + 3
16
&gt;&gt;&gt; from construct_check import check
&gt;&gt;&gt; # ban all loops
Expand All @@ -316,8 +312,6 @@ <h3 id="q2-digit-distance">Q2: Digit Distance</h3>
</script>
<br/>

</p>

<!-- -->


Expand All @@ -333,7 +327,7 @@ <h3 id="q3-interleaved-sum">Q3: Interleaved Sum</h3>
returns <code>1 + 2*2 + 3 + 4*4 + 5 = 29</code>.</p>

<blockquote><p><strong>Important:</strong> Implement this function without using any loops or directly testing if a number
is odd or even -- aka modulos (<code>%</code>) are not allowed! Instead of directly checking whether a number
is odd or even (no using <code>%</code>). Instead of directly checking whether a number
is even or odd, start with 1, which you know is an odd number.</p>

<p><strong>Hint:</strong> Introduce an inner helper function that takes an odd number <code>k</code> and
Expand Down Expand Up @@ -392,7 +386,7 @@ <h1 id="submit-assignment">Submit Assignment</h1>
<h1 id="just-for-fun-questions">Just For Fun Questions</h1>


<p>The questions below are out of scope for 61A. You can try them if you want an extra challenge, but they're just puzzles that are not required for the course. Almost all students will skip them, and that's fine. We will <strong>not</strong> be prioritizing support for these questions on Ed or during Office Hours.</p>
<p>The questions below are optional and not representative of exam questions. You can try them if you want an extra challenge, but they're just puzzles that are not required for the course. Almost all students will skip them, and that's fine. We will <strong>not</strong> be prioritizing support for these questions on Ed or during office hours.</p>


<h3 id="q4-towers-of-hanoi">Q4: Towers of Hanoi</h3>
Expand Down
Loading

0 comments on commit 3e256c9

Please sign in to comment.