Time: Tue 10/17/17 5 pm
Draw the environment diagram that results from executing the code after the entire program is finished. No errors occur during the execution of this example.
def scramble(egg):
return [egg, over(egg)]
def over(easy):
easy[1] = [[easy], 2]
return list(easy[1])
egg = scramble([12, 24])
Implement the look
method of the Dress
class. The look
method returns a Dress
instance's current color when the number of times that the instance's look
method has ever been invoked evenly divides the total number times that the look
method of any Dress
instance has ever been invoked. Otherwise, the instance's color
changes to the most recently returned color from any call to look
, and None
is returned.
class Dress:
"""
What color is the dress?
>>> blue = Dress('blue')
>>> blue.look()
'blue'
>>> gold = Dress('gold')
>>> gold.look()
'gold'
>>> blue.look() # 2 does not evenly divide 3; changes to gold
>>> Dress('black').look()
'black'
>>> gold.look() # 2 does not evenly divide 5; changes to black
>>> gold.look() # 3 evenly divides 6
'black'
>>> Dress('white').look()
'white'
>>> gold.look() # 4 evenly divides 8
'black'
>>> blue.look() # 3 evenly divides 9
'gold'
"""
seen = 0
color = None
def __init__(self, color):
self.color = color
self.seen = 0
def look(self):
=
=
if :
=
return
else:
=
Implement decrypt
, which takes a string s
and a dictionary d
that contains words as values and their secret codes as keys. It returns a list of all possible ways in which s
can be decoded by splitting it into secret codes and separating the corresponding words by spaces.
def decrypt(s, d):
"""
List all possible decoded strings of s.
>>> codes = {
... 'alan': 'spooky',
... 'al': 'drink',
... 'antu': 'your',
... 'turing': 'ghosts',
... 'tur': 'scary',
... 'ing': 'skeletons',
... 'ring': 'ovaltine'
... }
>>> decrypt('alanturing', codes)
['drink your ovaltine', 'spooky ghosts', 'spooky scary skeletons']
"""
if s == '':
return []
ms = []
if :
ms.append()
for k in :
first , suffix = s[:k], s[k:]
if :
for rest in :
ms.append()
return ms