CPSC150A
Scientific Computing

Activity 11

Lists

Create List

Write a function create_list(item_1, item_2, item_3, item_4, item_5), which takes 5 parameters of any type. The function should create a list with item_1 as the first element, etc.

Example

Test Code Output
print(create_list(1, 2, 3, 4, 5))) [1, 2, 3, 4, 5]
print(create_list(‘one’, ‘two’, ‘three’, ‘four’, ‘five’)) [‘one’, ‘two’, ‘three’, ‘four’, ‘five’]

Add Ends

Write a function add_ends(a_list), which takes a non-empty list of numbers as a parameter. Your function should return the sum of the first element and the last element from the list.

Example

Test Code Output
print(add_ends([1, 2, 3])) 4
print(add_ends([1])) 2

Birthday Paradox

Suppose a classmate offers to bet you $100 that there are two people in the class that have the same birthday. It seems that the odds would be pretty slim. However, if you are putting $100 on this bet, you might want to figure out your odds of winning.

Details

Create a program that determines the number of people it would take for the probablility of two people in the group to have the same birthday to exceed 50%. The program should print the probability for all groups, from size 2 up to the answer.

The program should not compute the exact probability, instead it should approximate the probability with repeated simulation. That is, repeatedly create a group of birthdays the specified size using a list. The number of times that there are at least identical birthdays divided by the number of times the simulation is repeated is an estimate for the probability. The more times the simulation is repeated, the more accurate it will be, but about 10000 iterations is sufficient.

Example

Group Size: 2 Probability: 0.00282
Group Size: 3 Probability: 0.00836
Group Size: 4 Probability: 0.01613
Group Size: 5 Probability: 0.02668
Group Size: 6 Probability: 0.04042
Group Size: 7 Probability: 0.05691
Group Size: 8 Probability: 0.07511
Group Size: 9 Probability: 0.09479
Group Size: 10 Probability: 0.11764
.
.
.
more lines here

Hint

Break the problem into mutitple parts using functions. For example:

  • Create the function simulate(group_size). The function should similate a group of the specified size by creating a list of random integers between 0 and 364. Each integer represents a person’s birthday, 0 is January 1, 1 is January 2, etc. The function should return True if any two people in the group have the same birthday. To do this, use the list count method to check if there are any numbers in the list that occur more than once.

  • Create the function probability(group_size). The function should return an estimate of probability that two people in the specified group size have the same birthday. The function should call the simulate function 10000 times, counting the number of times it returns True. The probability can be computed by dividing the count by 10000.

With these two functions printing all of the probabilities is simple. Create a while loop that will compute and print the probability for a group size. The group size should increase by 1 every iteration of the loop and the loop should stop when the probability exceeds 0.5.

Submission

Please show your source code and run your programs. Only programs that have perfect functionality will be accepted as complete.