[pythonvis] Script Calculating Probability of Duplicate Birthdays Using a Dictionary

  • From: "Jeffrey Turner" <jturner522@xxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Mon, 22 Jun 2015 12:26:32 -0400

Hello,

This is a rework of a previously submitted solution. The exercise was to
revise the previous solution so it used a dictionary to test for duplicates.

The script is also attached for anyone who would like to run it.

Enjoy,
JDog

#Script to calculate the probability that there will be a match of birthdays
of any 23 people.

from random import randint # to generate random integers

'''Modified function to test for duplicates using a dictionary. Start with
an empty dictionary. Start a for loop and check to see if the list items are
in it. The first one that is, return True.'''
def hasDups(t):
d = {} #dictionary to hold the values
for i in t:
if i in d:
return True
#End of if in d test, so we know it was not in the dictionary
d[i] = True #So now we put it in and set an arbitrary value
#End of For loop
return False #If we drop past the for loop, there are no duplicates
#End of function

#Function to create a list holding 23 integers
def get23Rand():
t = [] #list to hold the 23 integers
for i in xrange(23): #do the following 23 times
t.append(randint(1,366)) # get an integer corresponding to a
birthday and append it to the list
#End of for loop
return t
#end of function

match = 0 # to tally number of lists with duplicates
noMatch = 0 # to tally number of lists with nno duplicates

for j in xrange(100000): #Run the test 100,000 times
bDays = get23Rand() #get a list of 23 birthdays (really integers)
if hasDups(bDays):
match += 1
else:
noMatch += 1
#end of for loop
print str(match)+' match and '+str(noMatch)+' without equals
'+str(match/1000)+' percent.'
#Script to calculate the probability that there will be a match of birthdays of
any 23 people.

from random import randint # to generate random integers

'''Modified function to test for duplicates using a dictionary. Start with an
empty dictionary. Start a for loop and check to see if the list items are in
it. The first one that is, return True.'''
def hasDups(t):
d = {} #dictionary to hold the values
for i in t:
if i in d:
return True
#End of if in d test, so we know it was not in the dictionary
d[i] = True #So now we put it in and set an arbitrary value
#End of For loop
return False #If we drop past the for loop, there are no duplicates
#End of function

#Function to create a list holding 23 integers
def get23Rand():
t = [] #list to hold the 23 integers
for i in xrange(23): #do the following 23 times
t.append(randint(1,366)) # get an integer corresponding to a birthday
and append it to the list
#End of for loop
return t
#end of function

match = 0 # to tally number of lists with duplicates
noMatch = 0 # to tally number of lists with nno duplicates

for j in xrange(100000): #Run the test 100,000 times
bDays = get23Rand() #get a list of 23 birthdays (really integers)
if hasDups(bDays):
match += 1
else:
noMatch += 1
#end of for loop
print str(match)+' match and '+str(noMatch)+' without equals
'+str(match/1000)+' percent.'

Other related posts: