I\'m having a lot of trouble with this program. Could you upload your code so I
ID: 3589798 • Letter: I
Question
I'm having a lot of trouble with this program. Could you upload your code so I can compare and see what I am doing wrong? Thank you in advance!
The "birthday problem" concerns the probability that in a group of people, two people will have the same birthday. Of course, as the size of the group increases, the probability of two people sharing the same birthday increases as well. Surprisingly, in a group of just 23 people, there is a 50% probability that at least two people will have the same birthday! The probability of two people in a group of n people having the same birthday may be calculated as follows:
p(n) = 1 - 365/365 * 364/365 * 363/365 * ... * (365 - n + 1)/365
For example, in a group of 4 people, the probability of two people having the same birthday would be calculated as follows:
p(4) = 1 - 365/365 * 364/365 * 363/365 * (365 - 4 + 1)/365 =
1 - 365/365 * 364/365 * 363/365 * 362/365
= .016355912466550215
Requirements
Write a program named Birthdays.py that prompts the user for the minimum and maximum number of people and then outputs the probability that two people will have the same birthday for every number between the minimum and maximum. The probability values should be calculated using the above formula, and displayed with four decimal places and rounded to the nearest decimal place, as shown below.
For example (the red ink highlights the user input, the blue ink highlights the expected output),
Design Your program must prompt the user for the input values and use the method given below to calculate each probability. Your program will output each probability with 4 decimal places and rounded to the nearest decimal place. The output must be displayed in columns as shown above. Your should define a calculate_probability function and documented as shown below:
def calculate_probability(n):
""" Calculates probability that two people in a group of people will have the same birthday
:param n: integer, number of people
:return: float, probability that two people will have the same birthday
"""
Implementation
Use the following named (constant) variable in your program rather than a magic number.
o DAYS_PER_YEAR = 365; Your program must use a loop.
The method above should be called from within the loop.
Your methods must return the correct probability value as a double for all reasonable input values, not just the ones used to create the table above.
Output the probability values with 4 decimal places and rounded to the last decimal place by using a print statement to format the value. For example, the following statement formats an amount so that it is rounded, has 4 decimal places, and takes up a total of 20 spaces:
print("%20.4f" % 3.1415926) You can also use the print statement to help with formatting the number of people for each probability value.
Study the code below to learn more about string formatting. print("%10d, %10d" % (55, 777)) print("%10.4f, %10.4f" % (55.55, 0.7777777777777777))
Minimum númber of people: 4 Maximum number of people: 25 Probability of Two People Having Number of People the Same Birthday 4 0. 0164 0. 0271 0. 0405 0. 0562 0.0743 0. 0946 0. 1169 0. 1411 0. 1670 0.1944 0. 2231 0. 2529 0. 2836 0. 3150 0. 3469 0. 3791 0.4114 0. 4437 0. 4757 0. 5073 0. 5383 9 10 12 13 14 15 16 17 18 19 20 21 23 24Explanation / Answer
For python 2.x
from __future__ import division
DAYS_PER_YEAR = 365
def calculate_probability(n):
temp=1
for i in range(1,n+1):
temp=temp*((DAYS_PER_YEAR-i+1)/DAYS_PER_YEAR)
return 1-temp
x=input('Enter the minimum number ')
y=input('Enter the maximum number ')
for i in range(x,y):
ans=calculate_probability(i)
print "For number ",i,",probability is %.4f" % round(ans, 4)
For Python 3.x
from __future__ import division
DAYS_PER_YEAR = 365
def calculate_probability(n):
temp=1
for i in range(1,n+1):
temp=temp*((DAYS_PER_YEAR-i+1)/DAYS_PER_YEAR)
return 1-temp
x=int(input('Enter the minimum number '))
y=int(input('Enter the maximum number '))
for i in range(x,y):
ans=calculate_probability(i)
print ("For number "+str(i)+",probability is %.4f" % round(ans, 4))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.