Python 3.3: Birthday Probability
python
Solution
You should create the list like this. `test` isn't a very useful name. Consider something like `make_birthday_list` instead
def test(count):
return [random.randint(1, 365) for x in range(count)]
The test in duplicates already evaluates to a bool, so you can just do this. It's not a good idea to use `l` as a variable name (looks too much like `1`) so I changed it to `the_list`
def duplicates(the_list):
return len(the_list)!=len(set(the_list))
You'll have to run the test over and over to get the probability
eg.
num_samples = 10000
for count in range(100):
dup = 0
for test_number in range(num_samples):
the_list = make_birthday_list(count)
if duplicates(the_list):
dup += 1
print(count, dup/num_samples) # / returns a float in Python3
Problem
Trying to do the Birthday Program in python. Being a beginner in Python, I am having some trouble. - Function duplicates(l) that takes a list l and returns True if it has a duplicate element, and False if it does not. - Function test(count) generates a list of count random integers between 1 and 365 . Function duplicates(l) will test for duplicates. - Function probability(count, num) runs num tests of count people, and counts the number of tests with duplicates. It returns the fraction of the tests with duplicates - the number of duplicates divided by num. results should look like: For 2 people, the probability of 2 birthdays is 0.002. For 3 people, the probability of 2 birthdays is 0.008. and so on... Stuck on step 2 & 3: ``` import random # not sure what to put for count count = [random.randint(1, 365)] def duplicates(l): if len(l)!=len(set(l)): return True else: return False def test(count): return [random.randint(1, 365)] #def probability(count,num): ``` I believe I did step one correctly, but I'm not sure where to go from here.