Extra Practice for Exam 1

You are not logged in.

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 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:

  • If both are positive, result[i] is the sum of the first digits in each.
  • If both are negative, result[i] is the sum of the last digits in each.
  • If only one is positive and the other is not, result[i] is the result when you divide the last digit of the positive n by the first digit of the positive n.

For example, if 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].
  • etc.

Note: 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:

  • a 6 is rolled (win), or
  • three odd numbers have been rolled (lose).

Write a function play_game() that simulates one game and returns:

  • True if the game ends in a win;
  • False if the game ends in a loss.

You may assume the 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 estimate_win_probability(num_trials) that runs num_trials independent games and returns an estimate of the probability of winning.

Note: Copy your 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.
    """

Question 4. The following functions specify different step behaviors for the 2-D random walk we saw in lecture. Random walks of lengths ranging from 10 to 10,000 were simulated using each step function, and the final distances from the origin, averaged over 100 trials, are plotted below. (Note the logarithmic scales on the axes.)
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)

Specify which step functions correspond to which lines in the plot, in the following order by color: gray, yellow, blue, red.

Question 5. Consider the code below.
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)

Which of the following are true? (Select all that apply)