In: Computer Science
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >= 0 or i < 2: data.append(thing + i) print(len(stuff)) if more_stuff is not None: data += more_stuff print(data)
Write a Python statement that calls silly and results in the output (from within silly):
4 [5, 3, 1, 5, -3]
stuff = [5, 2, 0, 2]
more_stuff = [-3]
silly(stuff, more_stuff)
Q2.
Function silly2 is defined as:
def silly2(nums, indices, extra=None): print(len(nums), len(indices)) new_indices = [] for i in indices: if i >= 0 and i < len(nums): new_indices.append(i) if extra: new_indices.append(extra) try: for index in new_indices: if index < len(nums): print(nums[index]) except IndexError: print("We're dead, Fred")
Write a single Python statement that calls silly2 and results in the output:
2 2 We're dead, Fred
1)
CODE
def silly(stuff, more_stuff=None):
data = []
for i, thing in enumerate(stuff):
if thing >= 0 or i < 2:
data.append(thing + i)
print(len(stuff))
if more_stuff is not None:
data += more_stuff
print(data)
stuff = [5, 2, 0, 2]
more_stuff = [-3]
silly(stuff, more_stuff)
2)
CODE
def silly2(nums, indices, extra=None):
print(len(nums), len(indices))
new_indices = []
for i in indices:
if i >= 0 and i < len(nums):
new_indices.append(i)
if extra:
new_indices.append(extra)
try:
for index in new_indices:
if index < len(nums):
print(nums[index])
except IndexError:
print("We're dead, Fred")
nums = [1, 2]
indices = [0, 1]
silly2(nums, indices, -3)