""" Solution to the birthday exercise Think Python Allen B. Downey """ import random def has_duplicates(t): """return True if any element appears more than once in (t), False otherwise""" s = t[:] s.sort() for i in range(len(s)-1): if s[i] == s[i+1]: return True return False def random_bdays(n): """return a list of integers between 1 and 365, with length (n)""" t = [] for i in range(n): bday = random.randint(1, 365) t.append(bday) return t def count_matches(students, samples): """generates (samples) samples of (students) students, and count how many of them have at least one pair of students with the same bday""" population = range(1, 35) count = 0 for i in range(samples): t = random_bdays(students) if has_duplicates(t): count += 1 return count """run the birthday simulation 1000 times and print the number of matches""" x = count_matches(23, 1000) print x