Add Ends
Write the function add_ends(a_list)
that computes
the sum of the first and last elements of a list. The
parameter a_list is a list of numbers with at least two
elements. The function should return the sum of the first
element and the last element from the list.
Test Cases
print('Input: ?\tActual: ', add_ends(?), '\tExpected: ?')
Create List
Write the function create_list(item1, item2, item3, item4,
item5)
that returns a new list containing the five
parameters where item1 is the first
element, item2 is the second element, etc.
Test Cases
print('Input: ?, ?, ?, ?, ?\tActual: ', create_list(?, ?, ?, ?, ?), '\tExpected: ?')
Batting Average
The book, and later movie, Moneyball described how the use of statistics changed baseball. Using statistics like batting average can help mangagers make decisions. Batting average is the ratio of hits to at bats. Computing a player's batting average requires counting the different types of hits a player makes.
Details
Write the Python function
batting_average(appearances)
that computes a player's
batting average. The paramete appearances, is a list of
integers that encodes the results of a baseball player's appearances
at bat. Each appearance can be interpreted as follows:
4 | Home Run |
3 | Triple |
2 | Double |
1 | Single |
0 | Walk |
-1 | Out |
The function should return the batting average for the appearances in the list. The batting average can be computed using the following equations:
at_bats=appearances−walkshits=singles+doubles+triples+home_runsbatting_average=hits/at_bats
Test Cases
print('Input: [1, 1, -1]\tActual: ', batting_average([1, 1, -1]), '\tExpected: 0.6666666666') print('Input: [-1, 1, -1, -1, 1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 0, -1, 0, -1, -1, -1, 2, 1, -1, -1, -1, -1, 1, 0, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 0, -1, 1, 1, -1, -1, 2, 0, -1, -1, -1, -1, -1, -1, -1, 1, -1, 2, -1, -1, -1, -1, -1, -1, -1, 1]\tActual: ', batting_average([-1, 1, -1, -1, 1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 0, -1, 0, -1, -1, -1, 2, 1, -1, -1, -1, -1, 1, 0, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 0, -1, 1, 1, -1, -1, 2, 0, -1, -1, -1, -1, -1, -1, -1, 1, -1, 2, -1, -1, -1, -1, -1, -1, -1, 1]), '\tExpected: 0.25396825396825395')
To compute the batting average, the function needs to count the number of walks, singles, double, triples, and home runs. To do this, count the number of 0's, 1's, 2's, 3's, and 4's.
Challenge
Sabermetrics is the application of statistics to the management of
baseball teams that was popularized with the book
Moneyball. Sabermetric tries to improve upon the batting average
metric by not just computing a batter's hits but by also
incorporating a batter's contribution to the number of runs
scored. Write the function runs_created(appearances)
that returns the Sabermetrics runs created statistic for the
specified appearances list. The runs created stat can be computed
with the following formulas:
total_bases=(singles)+(2×doubles)+(3×triples)+(4×home_runs)runs_created=((hits+walks)×(total_bases))/plate_appearances
>>> print(runs_created([-1, 1, 4, -1, 0, 4, 1, -1, 0, 1, 0, 1, 2, 0, 1, 2, 1, -1, 1, 0, 0, 0, -1, 0])) 15.04166666666666
Birthday Paradox
Suppose someone comes into the class, and bets you $100 that some two people share a birthday within the individuals of the class. Do you share your birthday with anyone in the class? Is there a shared birthday between any two people in the class? It seems that the odds would be pretty slim. However, if you are putting up $100 on this bet, you might want to figure out your odds of winning this bet.
Details
Write the function shared_probability(group_size)
that computes the probability that two people in a group share a
birthday. The parameter group_size is a positive integer
that specifies the number of people in a group. The function
should return a floating point value in the range [0,1], the
probability that there is a shared birthday in a group of
size group_size
. A probability of 0.0 means
that there is a 0% chance and a probability of 1.0 means
that there is a 100% chance The function should not compute the
exact probability, instead it should approximate the probability
with repeated simulation.
To similate a group of people, create a list of group_size integers where each number represents a birthday. Ignoring leap years, there are 365 different birthdays, so the list should be filled with random numbers in the range [0,364]. If there are any duplicate numbers in the list, then two people share a birthday.
To compute the probability, run the simulation *many* times. The more times it runs the more accurate it will be. The probability is the average number of times that there are two people that share a birthday.
Once you have written and tested the function, use it to answe the following questions:
- What is the minimum group size that the probability exceeds 50%?
- What is the minimum group size that there is a virtual guarantee (0.9999) there is a shared birthday?
Test Cases
print('Input: 1\tActual: ', shared_probability(1), '\tExpected: 0.0') print('Input: 365\tActual: ', shared_probability(365), '\tExpected: 1.0')
Functions are your friends. They make writing complex functions
more simple by beaking it into more simple, easier to write
functions. Here are some functions that would make writing
the shared_probability(group_size)
function easier:
create_group(group_size)
- return a new list of group_size random integers in the range [0,364]are_duplicates(group)
- return whether there are any elements of group that are identical
Challenge
The above program assumes that birthdays are uniformly distributed: For a given person, their odds of being born on a particular date is the same for all dates. However, it is probably pretty obvious to you that this is not true in real life cases. In fact, you are much more likely to be born in the months July - October than any other month in the year.
How could you use the random number generator, which outputs
numbers in a uniform distribution, to generate some skewed
non-uniform random distribution? Try to redo
your shared_probability
function using this new,
non-uniform distribution of birthdays. How do the results for the
above questions change? Do they get higher, or lower? You may
assume, for simplicity sakes, that all months have 30 days for
this challenge portion.