Extra Practice for Exam 1
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Below are some additional questions that our TAs have written/collected for you to study. These are entirely optional; we do not record your scores, and these do not factor into your final grade.
These questions were chosen based on their relevance to topics we've covered so far. However, they should not be taken as representive of the types of questions on our exam, whether in format, difficulty, or coverage.
Question 1.
Given a list of integers For example, if Note: nums with length 2k, implement a function pairs(nums) that returns a new list result of length k.
Each element in result is made by every other consecutive pair of elements in nums, where:
result[i] is the sum of the first digits in each.result[i] is the sum of the last digits in each.result[i] is the result when you divide the last digit of the positive n by the first digit of the positive n.nums = [12, 34, -56, -67, -18, 93], then result should be [4, 13, 0.333], because:
result[0] is the sum of the first digits of nums[0] and nums[1].result[1] is the sum of the last digits of nums[2] and nums[3].nums will always have an even number of elements; result has length len(nums) // 2.def pairs(nums):
"""
Params:
nums: list of ints
Returns:
result: list of ints that follows the specification above.
"""
Question 2a.
What is printed out?
Your answer should be a valid Python expression.a = [1]
def foo(b):
b.append(2)
foo(a)
print(a)
Question 2b.
What is printed out?
Your answer should be a valid Python expression.a = 1
def foo(b):
b +=1
foo(a)
print(a)
Question 2c.
What is printed out?
Your answer should be a valid Python expression.a = 1
def foo(b):
b += 3
a = b
foo(a)
print(a)
Question 2d.
What is printed out?
Your answer should be a valid Python expression.a = ["a", "b"]
def foo(b):
c = b.copy()
c.append("c")
foo(a)
print(a)
Question 2e.
What is printed out?
Your answer should be a valid Python expression.a = ["a", "b"]
def foo(b):
c = b
b = a.copy()
c.append("c")
foo(a)
print(a)
Question 2f.
What is printed out?
Your answer should be a valid Python expression.a = [[2, 3], 1]
def foo(b):
b[0] = 1
foo(a)
print(a)
Question 2g.
What is printed out?
Your answer should be a valid Python expression.a = [[2, 3],1]
def foo(b):
c = b[:]
c[0] = 1
foo(a)
print(a)
Question 2h.
What is printed out?
Your answer should be a valid Python expression.a = [[2, 3],1]
def foo(b):
c = b[:]
c[0][0] = 1
foo(a)
print(a)
Question 2i.
What is printed out?
Your answer should be a valid Python expression.a = [[[2, 3], [2], 1]]
def foo(b):
c = b[:]
c[0][0].append(1)
foo(a)
print(a)
Question 3a.
A game consists of repeatedly rolling a fair six-sided die until either: Write a function You may assume the
play_game() that simulates one game and returns:
True if the game ends in a win;False if the game ends in a loss.random module is imported for you.def play_game():
"""
Simulate the dice game until the game ends.
Return:
True if the game ends in a win
False if the game ends in a loss
"""
Question 3b.
Building off your solution from Question 3a, write a function Note: Copy your estimate_win_probability(num_trials) that runs num_trials independent games and returns an estimate of the probability of winning.play_game() implementation from above.# copy your play_game() function here
def estimate_win_probability(num_trials):
"""
Simulate num_trials games. Return the estimate of the probability of winning.
"""
Specify which step functions correspond to which lines in the plot, in the following order by color: gray, yellow, blue, red.def take_step_v1(self):
step_choices = [(3, 0), (-3, 0)]
return random.choice(step_choices)
def take_step_v2(self):
step_choices = [(2, 0), (-2, 0), (0, 1), (0, -1)]
return random.choice(step_choices)
def take_step_v3(self):
step_choices = [(3, 0), (-2, 0), (0, 1), (0, -1)]
return random.choice(step_choices)
def take_step_v4(self):
step_choices = [(3, 0), (-2, 0), (0, 1), (0, -2)]
return random.choice(step_choices)
Which of the following are true? (Select all that apply)import random
def get_random_step():
return random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]])
def get_boring_step():
return [1, 0]
def move(current_pos, p, func_random, func_boring):
if random.random() < p:
step_choice = func_random
else:
step_choice = func_boring
delta = step_choice()
current_pos[0] += delta[0]
current_pos[1] += delta[1]
my_location = [0, 0]
move(my_location, 0.5, get_random_step, get_boring_step)