-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
202 additions
and
89 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,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 not shown.
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,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 | ||
""" |
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