Discussion 5: Probability and Simulation

← return to practice.dsc10.com


These problems are taken from past quizzes and exams. Work on them on paper, since the quizzes and exams you take in this course will also be on paper.

You will be working on these problems during your discussion section in groups of 2-4. Groupwork must be submitted on Gradescope (due Tuesday at 11:59pm), in addition to in-person attendance. Solutions will be made available after all discussion sections have concluded.

Note: We do not plan to cover all of these problems during the discussion section; the problems we don’t cover can be used for extra practice.


Problem 1

Lecture 8

In the ikea DataFrame, the first word of each string in the 'product' column represents the product line. For example the HEMNES line of products includes several different products, such as beds, dressers, and bedside tables.

The code below assigns a new column to the ikea DataFrame containing the product line associated with each product.

(ikea.assign(product_line = ikea.get('product')
                                .apply(extract_product_line)))


Problem 1.1

What are the input and output types of the extract_product_line function?


Problem 1.2

Complete the return statement in the extract_product_line function below.

For example, extract_product_line('HEMNES Daybed frame with 3 drawers, white, Twin') should return 'HEMNES'.

def extract_product_line(x):
    return _________

What goes in the blank?



Problem 2

Lecture 8

Complete the implementation of the to_minutes function below. This function takes as input a string formatted as 'x hr, y min' where x and y represent integers, and returns the corresponding number of minutes, as an integer (type int in Python).

For example, to_minutes('3 hr, 5 min') should return 185.

def to_minutes(time):
    first_split = time.split(' hr, ')
    second_split = first_split[1].split(' min')
    return _________

What goes in the blank?


Problem 3

Lecture 10

Consider the function tom_nook, defined below. Recall that if x is an integer, x % 2 is 0 if x is even and 1 if x is odd.

def tom_nook(crossing):
    bells = 0
    for nook in np.arange(crossing):
        if nook % 2 == 0:
            bells = bells + 1
        else:
            bells = bells - 2
    return bells

What value does tom_nook(8) evaluate to?


Problem 4

Lecture 11

You generate a three-digit number by randomly choosing each digit to be a number 0 through 9, inclusive. Each digit is equally likely to be chosen.


Problem 4.1

What is the probability you produce the number 027? Give your answer as a decimal number between 0 and 1 with no rounding.


Problem 4.2

What is the probability you produce a number with an odd digit in the middle position? For example, 250. Give your answer as a decimal number between 0 and 1 with no rounding.


Problem 4.3

What is the probability you produce a number with a 7 in it somewhere? Give your answer as a decimal number between 0 and 1 with no rounding.



Problem 5

Lecture 11

Suppose you are booking a flight and you have no control over which airline you fly on. Below is a table with multiple airlines and the probability of a flight being on a specific airline.

Airline Chance
Delta 0.4
United 0.3
American 0.2
All other airlines 0.1

The airline for one flight has no impact on the airline for another flight.

For this question, suppose that you schedule 3 flights for January 2022.


Problem 5.1

What is the probability that all 3 flights are on United? Give your answer as an exact decimal between 0 and 1 (not a Python expression).


Problem 5.2

What is the probability that all 3 flights are on Delta, or all on United, or all on American? Give your answer as an exact decimal between 0 and 1 (not a Python expression).


Problem 5.3

True or False: The probability that all 3 flights are on the same airline is equal to the probability you computed in the previous subpart.



Problem 6

Lecture 12

King Triton has boarded a Southwest flight. For in-flight refreshments, Southwest serves four types of cookies – chocolate chip, gingerbread, oatmeal, and peanut butter.

The flight attendant comes to King Triton with a box containing 10 cookies:

The flight attendant tells King Triton to grab 2 cookies out of the box without looking.

Fill in the blanks below to implement a simulation that estimates the probability that both of King Triton’s selected cookies are the same.

# 'cho' stands for chocolate chip, 'gin' stands for gingerbread,
# 'oat' stands for oatmeal, and 'pea' stands for peanut butter.

cookie_box = np.array(['cho', 'cho', 'cho', 'cho', 'gin', 
                       'gin', 'gin', 'oat', 'oat', 'pea'])

repetitions = 10000
prob_both_same = 0
for i in np.arange(repetitions):
    grab = np.random.choice(__(a)__)
    if __(b)__:
        prob_both_same = prob_both_same + 1
prob_both_same = __(c)__


Problem 6.1

What goes in blank (a)?


Problem 6.2

What goes in blank (b)?


Problem 6.3

What goes in blank (c)?



Problem 7

Lecture 11

Each individual penguin in our dataset is of a certain species (Adelie, Chinstrap, or Gentoo) and comes from a particular island in Antarctica (Biscoe, Dream, or Torgerson). There are 330 penguins in our dataset, grouped by species and island as shown below.


Suppose we pick one of these 330 penguins, uniformly at random, and name it Chester.


Problem 7.1

What is the probability that Chester comes from Dream island? Give your answer as a number between 0 and 1, rounded to three decimal places.


Problem 7.2

If we know that Chester comes from Dream island, what is the probability that Chester is an Adelie penguin? Give your answer as a number between 0 and 1, rounded to three decimal places.


Problem 7.3

If we know that Chester is not from Dream island, what is the probability that Chester is not an Adelie penguin? Give your answer as a number between 0 and 1, rounded to three decimal places.



Problem 8

Lecture 12

The fine print of the Sun God festival website says “Ticket does not guarantee entry. Venue subject to capacity restrictions.” RIMAC field, where the 2022 festival will be held, has a capacity of 20,000 people. Let’s say that UCSD distributes 21,000 tickets to Sun God 2022 because prior data shows that 5% of tickets distributed are never actually redeemed. Let’s suppose that each person with a ticket this year has a 5% chance of not attending (independently of all others). What is the probability that at least one student who has a ticket cannot get in due to the capacity restriction? Fill in the blanks in the code below so that prob_angry_student evaluates to an approximation of this probability.

num_angry = 0

for rep in np.arange(10000):
    # randomly choose 21000 elements from [True, False] such that 
    # True has probability 0.95, False has probability 0.05
    attending = np.random.choice([True, False], 21000, p=[0.95, 0.05])
    if __(a)__:
        __(b)__

prob_angry_student = __(c)__


Problem 8.1

What goes in the first blank?


Problem 8.2

What goes in the second blank?


Problem 8.3

What goes in the third blank?



Problem 9

Lecture 11


Problem 9.1

You’re definitely going to Sun God 2022, but you don’t want to go alone! Fortunately, you have n friends who promise to go with you. Unfortunately, your friends are somewhat flaky, and each has a probability p of actually going (independent of all others). What is the probability that you wind up going alone? Give your answer in terms of p and n.


Problem 9.2

In past Sun God festivals, sometimes artists that were part of the lineup have failed to show up! Let’s say there are n artists scheduled for Sun God 2022, and each artist has a probability p of showing up (independent of all others). What is the probability that the number of artists that show up is less than n, meaning somebody no-shows? Give your answer in terms of p and n.



Problem 10

Lecture 11

True or False: If you roll two dice, the probability of rolling two fives is the same as the probability of rolling a six and a three.


Problem 11

Lecture 11

The HAUGA bedroom furniture set includes two items, a bed frame and a bedside table. Suppose the amount of time it takes someone to assemble the bed frame is a random quantity drawn from the probability distribution below.

Time to assemble bed frame Probability
10 minutes 0.1
20 minutes 0.4
30 minutes 0.5

Similarly, the time it takes someone to assemble the bedside table is a random quantity, independent of the time it takes them to assemble the bed frame, drawn from the probability distribution below.

Time to assemble bedside table Probability
30 minutes 0.3
40 minutes 0.4
50 minutes 0.3


Problem 11.1

What is the probability that Stella assembles the bed frame in 10 minutes if we know it took her less than 30 minutes to assemble? Give your answer as a decimal between 0 and 1.


Problem 11.2

What is the probability that Ryland assembles the bedside table in 40 minutes if we know that it took him 30 minutes to assemble the bed frame? Give your answer as a decimal between 0 and 1


Problem 11.3

What is the probability that Jin assembles the complete HAUGA set in at most 60 minutes? Give your answer as a decimal between 0 and 1.



Problem 12

Lecture 11

King Triton had four children, and each of his four children started their own families. These four families organize a Triton family reunion each year. The compositions of the four families are as follows:

Suppose we choose one of the fifteen people at the Triton family reunion at random.


Problem 12.1

Given that the chosen individual is from a family with one child, what is the probability that they are from Family X? Give your answer as a simplified fraction.


Problem 12.2

Consider the events A and B, defined below.

True or False: Events A and B are independent.


Problem 12.3

Consider the events C and D, defined below.

True or False: Events C and D are independent.


Problem 12.4

At the reunion, the Tritons play a game that involves placing the four letters into a hat (W, X, Y, and Z, corresponding to the four families). Then, five times, they draw a letter from the hat, write it down on a piece of paper, and place it back into the hat.

Let p = \frac{1}{4} in the questions that follow.

What is the probability that Family W is selected all 5 times?



Problem 12.5

What is the probability that Family W is selected at least once?


Problem 12.6

What is the probability that Family W is selected exactly once, as the last family that is selected?



👋 Feedback: Find an error? Still confused? Have a suggestion? Let us know here.