-
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
143 additions
and
15 deletions.
There are no files selected for viewing
Binary file not shown.
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,95 @@ | ||
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 |
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