Skip to content

Commit

Permalink
Wed Oct 23 17:39:12 PDT 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Oct 24, 2024
1 parent 16afeda commit 5aca3b2
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 89 deletions.
123 changes: 36 additions & 87 deletions assets/slides/16.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,44 @@
from link import *


def length(s):
"""Return the number of elements in linked list s.
>>> length(Link(3, Link(4, Link(5))))
3
"""
if s is Link.empty:
return 0
else:
return 1 + length(s.rest)
t = [[1, 2], [3, 4]]
t[0].append(t[1:2])

def of(us):
def last(k):
"The last k items of us"
while k > 0:
result.append(us.pop())
k = k - 1
return result
return last

def surround(n, f):
"n is the first and last item of f(2)"
result = [n]
result = f(2)
result[0] = [n]
return result.append(n)

result = [1]
surround(3, of([4, 5, 6]))

def length_iter(s):
"""Return the number of elements in linked list s.
from link import *

>>> length_iter(Link(3, Link(4, Link(5))))
3
def nested_link():
"""
k = 0
while s is not Link.empty:
s, k = s.rest, k + 1
return k

def append(s, x):
"""Append x to the end of non-empty s and return None.
>>> s = Link(3, Link(4, Link(5)))
>>> append(s, 6)
>>> s = Link(2, Link(3, Link( 4 , Link(5))))
>>> t = Link(2, Link(3, Link( Link(4) , Link(5))))
>>> print(s)
<3 4 5 6>
"""
if s.rest:
append(s.rest, x)
else:
s.rest = Link(x)

def append_iter(s, x):
"""Append x to the end of non-empty s and return None.
>>> s = Link(3, Link(4, Link(5)))
>>> append_iter(s, 6)
<2 3 4 5>
>>> print(t)
<2 3 <4> 5>
>>> s = Link(Link(8), Link(Link(4, Link(6, Link(Link(7)))), Link(5)))
>>> print(s)
<3 4 5 6>
"""
while s.rest:
s = s.rest
s.rest = Link(x)


def pop(s, i):
"""Remove and return element i from linked list s for positive i.
>>> t = Link(3, Link(4, Link(5, Link(6))))
>>> pop(t, 2)
5
>>> pop(t, 2)
6
>>> pop(t, 1)
4
>>> t
Link(3)
<<8> <4 6 <7>> 5>
>>> s.first.first
8
>>> s.rest.first.rest.rest.first
Link(7)
>>> s.rest.first.rest.rest.first.first
7
"""
assert i > 0 and i < length(s)
for x in range(i-1):
s = s.rest
result = s.rest.first
s.rest = s.rest.rest
return result


def range_link(start, end):
"""Return a Link containing consecutive integers from start to end.
>>> range_link(3, 6)
Link(3, Link(4, Link(5)))
"""
if start >= end:
return Link.empty
else:
return Link(start, range_link(start + 1, end))

def range_link_iter(start, end):
"""Return a Link containing consecutive integers from start to end.
>>> range_link_iter(3, 6)
Link(3, Link(4, Link(5)))
"""
s = Link.empty
k = end - 1
while k >= start:
s = Link(k, s)
k -= 1
return s
Binary file added assets/slides/17-Iterators_1pp.pdf
Binary file not shown.
162 changes: 162 additions & 0 deletions assets/slides/17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
from link import *

def length(s):
"""Return the number of elements in linked list s.
>>> length(Link(3, Link(4, Link(5))))
3
"""
if s is Link.empty:
return 0
else:
return 1 + length(s.rest)

def length_iter(s):
"""Return the number of elements in linked list s.
>>> length_iter(Link(3, Link(4, Link(5))))
3
"""
k = 0
while s is not Link.empty:
s, k = s.rest, k + 1
return k

def append(s, x):
"""Append x to the end of non-empty s and return None.
>>> s = Link(3, Link(4, Link(5)))
>>> append(s, 6)
>>> print(s)
<3 4 5 6>
"""
if s.rest:
append(s.rest, x)
else:
s.rest = Link(x)

def append_iter(s, x):
"""Append x to the end of non-empty s and return None.
>>> s = Link(3, Link(4, Link(5)))
>>> append_iter(s, 6)
>>> print(s)
<3 4 5 6>
"""
while s.rest:
s = s.rest
s.rest = Link(x)


def pop(s, i):
"""Remove and return element i from linked list s for positive i.
>>> t = Link(3, Link(4, Link(5, Link(6))))
>>> pop(t, 2)
5
>>> pop(t, 2)
6
>>> pop(t, 1)
4
>>> t
Link(3)
"""
assert i > 0 and i < length(s)
for x in range(i-1):
s = s.rest
result = s.rest.first
s.rest = s.rest.rest
return result


def range_link(start, end):
"""Return a Link containing consecutive integers from start to end.
>>> range_link(3, 6)
Link(3, Link(4, Link(5)))
"""
if start >= end:
return Link.empty
else:
return Link(start, range_link(start + 1, end))

def range_link_iter(start, end):
"""Return a Link containing consecutive integers from start to end.
>>> range_link_iter(3, 6)
Link(3, Link(4, Link(5)))
"""
s = Link.empty
k = end - 1
while k >= start:
s = Link(k, s)
k -= 1
return s

# Iterators

def iterator_demos():
"""Using iterators
>>> s = [[1, 2], 3, 4, 5]
>>> next(s)
Traceback (most recent call last):
...
TypeError: 'list' object is not an iterator
>>> t = iter(s)
>>> next(t)
[1, 2]
>>> next(t)
3
>>> u = iter(s)
>>> next(u)
[1, 2]
>>> list(t)
[4, 5]
>>> a = [1, 2, 3]
>>> b = [a, 4]
>>> c = iter(a)
>>> d = c
>>> print(next(c))
1
>>> print(next(d))
2
>>> b
[[1, 2, 3], 4]
"""

def double(x):
print('***', x, '=>', 2*x, '***')
return 2*x

def built_in_demo():
"""Using built-in sequence functions.
>>> bcd = ['b', 'c', 'd']
>>> [x.upper() for x in bcd]
['B', 'C', 'D']
>>> caps = map(lambda x: x.upper(), bcd)
>>> next(caps)
'B'
>>> next(caps)
'C'
>>> s = range(3, 7)
>>> doubled = map(double, s)
>>> next(doubled)
*** 3 => 6 ***
6
>>> next(doubled)
*** 4 => 8 ***
8
>>> list(doubled)
*** 5 => 10 ***
*** 6 => 12 ***
[10, 12]
>>> all(map(double, range(-3, 3)))
*** -3 => -6 ***
*** -2 => -4 ***
*** -1 => -2 ***
*** 0 => 0 ***
False
"""
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ <h2 class="frontpage-header">Announcements: Wednesday, October 23</h2>
<ul>
<li>Arrive by 8pm to find your seat; the exam will start at 8:10pm.</li>
<li>Seat assignments will be emailed to you before the exam (probably Tuesday)</li>
<li>Complete the alteration request form (coming very soon) by Sunday, 10/27 at 11:59pm for any seating/timing requests (including DSP students).</li>
<li>Complete the <a href="https://forms.gle/T5swr13pQX1ZiD937">alteration request form</a> by Sunday, 10/27 at 11:59pm for any seating/timing requests (including DSP students).</li>
<li>The exam covers material in the videos through Monday 10/21 (Linked Lists).</li>
<li>You may bring up to 2 2-sided sheets of notes.</li>
<li>The <a href="https://c88c.org/fa24/assets/pdfs/c88c-mt-study-guide.pdf">midterm study guide</a> will be printed for you and provided with your exam.</li>
Expand Down Expand Up @@ -1245,6 +1245,8 @@ <h2 class="frontpage-header">Calendar</h2>

<ul class="list-inline">
<li><a href="https://www.youtube.com/watch?v=On-kFyFp8HY&list=PL6BsET-8jgYW7stiut93PHxGP6_QgBzVQ&ab_channel=JohnDeNero" class="label label-outline" target="_blank">Videos</a></li>
<li><a href="/fa24/assets/slides/17-Iterators_1pp.pdf" class="label label-outline">Slides (1pp)</a></li>
<li><a href="/fa24/assets/slides/17.py" class="label label-outline">17.py</a></li>
</ul>
</td>
<td>
Expand Down Expand Up @@ -1614,7 +1616,7 @@ <h3><a href="/articles/about">Policies</a></h3>

});

let build_time = new Date(1000 * 1729713393.664832);
let build_time = new Date(1000 * 1729730159.008139);
</script>
<script>
$('.alwaystoggle').css('display', 'inline-block');
Expand Down

0 comments on commit 5aca3b2

Please sign in to comment.