Code
from datascience import *
import numpy as np
Tiffany has a bag with three marbles. One marble is orange, and the other two are purple. For each round of a game, she draws from the bag 10 times with replacement. She wins the round by drawing at least one orange marble.
from datascience import *
import numpy as np
Write a function to simulate one round of Tiffany’s game. The function should return True
if she wins and False
if she loses.
def one_round():
= ____________________
bag = ____________________
one_sim = ____________________
num_orange return ____________________
def one_round():
= make_array('purple', 'purple', 'orange')
bag = np.random.choice(bag, 10)
one_sim = sum(one_sim == 'orange')
num_orange return num_orange >= 1
Instead of sum
, students can also use np.count_nonzero()
. They can also use sample_proportions
with probabilities (2/3, 1/3).
one_round()
True
Using the function you just defined in part a, finish the following code to help Tiffany simulate 100 rounds of the game and assign the variable win_proportion
to the proportion of rounds she wins.
= 0
count for ____________________:
if ____________________:
____________________= ____________________ win_proportion
= 0
count for i in np.arange(100):
if one_round():
= count + 1
count = count / 100 win_proportion
win_proportion
0.98
if one_round()
” works by incrementing the count variable when one_round()
evaluates to True
(and will not if one_round()
evaluates to False
).
For any one draw, what is the probability that Tiffany draws a purple marble?
For any individual round, what is the probability that Tiffany loses?
For any individual round, what is the probability that Tiffany wins?
Using the complement rule:
P(Tiffany wins one round) = 1 - P(Tiffany loses one round) = 1 - \((\frac{2}{3})^{10}\)
When we observe something different from what we expect in real life (i.e., four 3’s in six rolls of a fair die), a natural question to ask is ”Was this unexpected behavior due to random chance, or something else?”
The % operator returns the remainder if you divide by a certain number (e.g., \(11 \% 5 = 1\)). If a number n
is odd, what will n % 2
return?
It will return 1. Note that if a number is even, n%2
will return 0.
= 11
n % 2 n
1
Remember, we can “multiply” a string by a certain number (e.g., 'data8' * 2
). What is the result of doing so? Take 'data8' * 2
as an example.
It will return 'data8data8'
. More generally speaking, it will concatenate the string with itself that number of times with no empty space between them.
'data8' * 2
'data8data8'
What will the following code output? Format your answer in the same way you expect Python to output the result of running the following code.
for c in np.arange(4):
if c < 2:
print("Chocolate?")
if c % 2 == 0:
print("Yes sir! With or without nuts?")
else:
print("CHOCOLATE" + ("!" * c))
for c in np.arange(4):
if c < 2:
print("Chocolate?")
if c % 2 == 0:
print("Yes sir! With or without nuts?")
else:
print("CHOCOLATE" + ("!" * c))
Chocolate?
Yes sir! With or without nuts?
Chocolate?
CHOCOLATE!!
CHOCOLATE!!!
The count_evens
function takes in an array of integers and returns the number of even integers in the array. Use a combination of iteration and conditionals to complete the skeleton code below.
def count_evens(an_array):
= 0
num_evens for ____________________:
if ____________________:
____________________return ____________________
def count_evens(an_array):
= 0
num_evens for num in an_array:
if num % 2 == 0:
= num_evens + 1
num_evens return num_evens
= make_array(1, 2, 3, 4, 5, 6, 7)
arr count_evens(arr)
3
Now, let’s see how we can write the same function using array operations instead of iteration.
def count_evens(an_array):
= ____________________
remainder_array return ____________________
def count_evens(an_array):
= an_array % 2
remainder_array return np.count_nonzero(remainder_array == 0)
= make_array(1, 2, 3, 4, 5, 6, 7)
arr count_evens(arr)
3
Alternate Solution:
def count_evens(an_array):
= an_array % 2
remainder_array return len(remainder_array) - sum(remainder_array)
= make_array(1, 2, 3, 4, 5, 6, 7)
arr count_evens(arr)
3