Homework 6 Solutions
Solution Files
You can find the solutions in hw06.py.
Required Questions
Getting Started Videos
These videos may provide some helpful direction for tackling the coding problems on this assignment.
To see these videos, you should be logged into your berkeley.edu email.
Q1: Shuffle
Implement shuffle
, which takes a sequence s
(such as a list or range) with
an even number of elements. It returns a new list that interleaves the
elements of the first half of s
with the elements of the second half. It does
not modify s
.
To interleave two sequences s0
and s1
is to create a new list containing
the first element of s0
, the first element of s1
, the second element of
s0
, the second element of s1
, and so on. For example, if s = [1, 2, 3, 4, 5, 6]
then s0 = [1, 2, 3]
and s1 = [4, 5, 6]
, and interleaving s0
and s1
would result in
[1, 4, 2, 5, 3, 6]
.
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
Use Ok to test your code:
python3 ok -q shuffle
Q2: Deep Map
Definition: A nested list of numbers is a list that contains numbers and
lists. It may contain only numbers, only lists, or a mixture of both. The lists
must also be nested lists of numbers. For example: [1, [2, [3]], 4]
, [1, 2,
3]
, and [[1, 2], [3, 4]]
are all nested lists of numbers.
Write a function deep_map
that takes two arguments: a nested list of numbers
s
and a one-argument function f
. It modifies s
in place by replacing each number in s with the result of
calling f
on that number.
Important:: deep_map
returns None
and should not create any new lists.
Hint:
type(a) == list
will evaluate toTrue
ifa
is a list.
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])
Use Ok to test your code:
python3 ok -q deep_map
Q3: Common Players
Implement the function common_players
. The common_players
function takes in a roster
dictionary that
maps players to their teams, and returns a new dictionary that maps teams to a list of players on that team.
The order of player names in the list does not matter.
def common_players(roster):
"""Returns a dictionary containing values along with a corresponding
list of keys that had that value from the original dictionary.
>>> full_roster = {
... "bob": "Team A",
... "barnum": "Team B",
... "beatrice": "Team C",
... "bernice": "Team B",
... "ben": "Team D",
... "belle": "Team A",
... "bill": "Team B",
... "bernie": "Team B",
... "baxter": "Team A"
... }
>>> player_dict = common_players(full_roster)
>>> type(player_dict) == dict
True
>>> for key, val in sorted(player_dict.items()):
... print(key, list(sorted(val)))
Team A ['baxter', 'belle', 'bob']
Team B ['barnum', 'bernice', 'bernie', 'bill']
Team C ['beatrice']
Team D ['ben']
"""
result_dict = {}
for player in roster:
team = roster[player]
if team in result_dict:
result_dict[team] += [player]
else:
result_dict[team] = [player]
return result_dict
Use Ok to test your code:
python3 ok -q common_players
Check Your Score Locally
You can locally check your score on each question of this assignment by running
python3 ok --score
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
Submit Assignment
Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.