Skip to content

Commit

Permalink
Mon Oct 21 13:04:29 PDT 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Oct 21, 2024
1 parent e084b1a commit 48f94f3
Show file tree
Hide file tree
Showing 15 changed files with 2,013 additions and 18 deletions.
2 changes: 1 addition & 1 deletion _internal/targets.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"due": "10/18/24", "link": "lab/lab07/", "name": "Lab 07: Object-Oriented Programming", "piazza_name": "Lab 07", "release": "10/14/24"}, {"due": "10/23/24", "link": "hw/hw07/", "name": "HW 07: Object-Oriented Programming", "piazza_name": "HW 07", "release": "10/16/24"}, {"due": "11/20/24", "link": "proj/ants/", "name": "Ants", "piazza_name": "Ants", "release": "10/16/24"}]
[{"due": "10/23/24", "link": "hw/hw07/", "name": "HW 07: Object-Oriented Programming", "piazza_name": "HW 07", "release": "10/16/24"}, {"due": "10/25/24", "link": "lab/lab08/", "name": "Lab 08: Inheritance, Linked Lists", "piazza_name": "Lab 08", "release": "10/21/24"}, {"due": "11/20/24", "link": "proj/ants/", "name": "Ants", "piazza_name": "Ants", "release": "10/16/24"}]
513 changes: 513 additions & 0 deletions disc/disc08/index.html

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions hw/sol-hw06/hw06.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Homework 6",
"endpoint": "cal/c88c/fa24/hw06",
"src": [
"hw06.py"
],
"tests": {
"hw*.py": "doctest",
"tests/*.py": "ok_test"
},
"default_tests": [
"shuffle",
"deep_map"
],
"protocols": [
"restore",
"file_contents",
"analytics",
"help",
"unlock",
"grading",
"backup"
]
}
50 changes: 50 additions & 0 deletions hw/sol-hw06/hw06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def shuffle(s):
"""Return a shuffled list that interleaves the two halves of s.
>>> shuffle(range(6))
[0, 3, 1, 4, 2, 5]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> shuffle(letters)
['a', 'e', 'b', 'f', 'c', 'g', 'd', 'h']
>>> shuffle(shuffle(letters))
['a', 'c', 'e', 'g', 'b', 'd', 'f', 'h']
>>> letters # Original list should not be modified
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
"""
assert len(s) % 2 == 0, 'len(seq) must be even'
half = len(s) // 2
shuffled = []
for i in range(half):
shuffled.append(s[i])
shuffled.append(s[half + i])
return shuffled


def deep_map(f, s):
"""Replace all non-list elements x with f(x) in the nested list s.
>>> six = [1, 2, [3, [4], 5], 6]
>>> deep_map(lambda x: x * x, six)
>>> six
[1, 4, [9, [16], 25], 36]
>>> # Check that you're not making new lists
>>> s = [3, [1, [4, [1]]]]
>>> s1 = s[1]
>>> s2 = s1[1]
>>> s3 = s2[1]
>>> deep_map(lambda x: x + 1, s)
>>> s
[4, [2, [5, [2]]]]
>>> s1 is s[1]
True
>>> s2 is s1[1]
True
>>> s3 is s2[1]
True
"""
for i in range(len(s)):
if type(s[i]) == list:
deep_map(f, s[i])
else:
s[i] = f(s[i])

Binary file added hw/sol-hw06/hw06.zip
Binary file not shown.
Loading

0 comments on commit 48f94f3

Please sign in to comment.