def simulations(num_birth): birthday = [] while num_birth > 0: birthday.append(list(mydate.generate_date(2014, 2017))) num_birth -= 1 result = mydate.remove_years(birthday) return result
def process_birthdays(year_month_days): processed_birthdays = [] month_days = mydate.remove_years(year_month_days) for birthday in month_days: birthday[0] = mydate.month_num_to_string(birthday[0]) birthday[1] = str(birthday[1]) processed_birthdays.append(birthday) return map(lambda x: ' '.join(x), processed_birthdays)
def generate_sims(number_of_sims, number_of_birthdays): number_of_trials_with_dups = 0 for i in range(int(number_of_sims)): birthdays = [] for j in range(int(number_of_birthdays)): birthdays.append(mydate.generate_date(1900,2019)) final = mydate.remove_years(birthdays) # print (final) duplicates = [] dup_counter = 0 while (len(final) > 0): current = final.pop() if current in final: duplicates.append(current) dup_counter += 1 while current in final: final.remove(current) duplicates_formatted = mydate.dates_to_strings(duplicates) if (dup_counter == 0): print("Trial #" + str(i + 1) + ": " + "No dates are the same.") elif (dup_counter == 1): number_of_trials_with_dups += 1 print("Trial #" + str(i + 1) + ": " + str(dup_counter) + " date occurs more than once! (" + str(duplicates_formatted).strip("[]").replace("'", "") + ")") else: number_of_trials_with_dups += 1 print("Trial #" + str(i + 1) + ": " + str(dup_counter) + " dates occur more than once! (" + str(duplicates_formatted).strip("''[]").replace("'", "") + ")") print("\nResults:\n=====") print("Out of " + str(number_of_sims) + " trials, " + str(number_of_trials_with_dups) + " had dates that were repeated") # percentage = str (100 * # float(number_of_trials_with_dups)/float(number_of_sims)) + "%" percentage = "{0:.2f}".format(100 * float(number_of_trials_with_dups)/float(number_of_sims)) # print ("Numerator: " + str(number_of_trials_with_dups)) # print("Denomentaor: " + number_of_sims) # print ("Percentage: " + str(percentage)) print("We can conclude that you have a " + percentage + " chance of sharing a birthday with someone if you are in a group of " + number_of_birthdays + " people")
def main(): run_count = input("How many times should I run the simulation?\n") bday_count = input("How many birthdays should I generate per trial?\n") trials_with_rpt = 0 for i in range(int(run_count)): bdays = [] for j in range(int(bday_count)): bdays.append(mydate.generate_date(1997, 2017)) bdays = mydate.remove_years(bdays) # remove years bdays = mydate.dates_to_strings(bdays) # convert to string unique_dates = set(bdays) # removes repeats from bdays # loop compares bdays to unique_dates to create a list of # the dates of same-day birthdays repeat_day_list = [] for days in unique_dates: if (bdays.count(days) > 1): repeat_day_list.append(days) repeat_count = len(repeat_day_list) repeat_day_str = ', '.join(map(str, repeat_day_list)) if (repeat_count > 1): print("Trial #{}: {} dates occur more than once! ({})".format( i + 1, repeat_count, repeat_day_str)) trials_with_rpt += 1 elif (repeat_count == 1): print("Trial #{}: {} date occurs more than once! ({})".format( i + 1, repeat_count, repeat_day_str)) trials_with_rpt += 1 else: print("Trial #{}: No dates are the same.".format(i + 1)) rpt_prob = (trials_with_rpt / int(run_count)) * 100 print("\nResults:\n=====") print("Out of {} trials, {} had dates that were repeated.".format( int(run_count), trials_with_rpt)) print("""We can conclude that you have a {:.2f}% chance of sharing a birthday with someone if you are in a group of {} people.""".format( rpt_prob, int(bday_count)))
run_count = 0 start_year = 1996 end_year = 2000 probability_info = [] # generate the number of birthdays specified while (run_count < run_occurance): dates_full = [] dates_counter = 0 while (dates_counter < birthdays_to_generate): dates_full.append(tuple(mydate.generate_date(start_year, end_year))) dates_counter += 1 # remove the years from the dates dates = mydate.remove_years(dates_full) # find the dates that occur more than once duplicate_dates = set() unique_dates = [] dupes = [] dupes_full = [] for x in dates: if x not in duplicate_dates: unique_dates.append(x) duplicate_dates.add(x) else: dupes.append(x) dupes_full.append(dates_full[dates.index(x)]) # print trial number, # of dates that occur more than once, and a comma separated list of the duplicate dates, in parentheses
# Homework 01 import mydate print("How many times should I run the simulation?") trials = input() print("How many birthdays should I generate per trial?") entries = input() birthdays = [] count = 0 # count the number of trials which have duplicate birthdays for x in range(int(trials)): for y in range(int(entries)): birthdays.append(mydate.generate_date(1900, 2018)) new_birthdays = mydate.remove_years(birthdays) #print(new_birthdays) duplicate = [ x for n, x in enumerate(new_birthdays) if x in new_birthdays[:n] ] # this line is taken from stackoverflow regarding how to make a new list of duplicates if len(duplicate) == 0: print("Trial #" + str(x + 1) + ": No dates are the same.") else: count += 1 if len(duplicate) > 1: print("Trial #" + str(x + 1) + ": " + str(len(duplicate)) + " dates occur more than once! (" + mydate.dates_to_strings(duplicate) + ")") else: print("Trial #" + str(x + 1) + ": " + str(len(duplicate)) + " date occurs more than once! (" +
times = int(input()) #initialize arrays to appropriate size birthdates = [None] * (times) duplicates = [None] * (times) trial_counter = 0 for repeats in range(trials): for tries in range(times): birthdates[tries] = md.generate_date(1900, 2018) #2 md.remove_years(birthdates) #3. Catch duplicate birthdays and store in this array duplicates = md.duplicate(birthdates) #4. Print out the duplicate birthdays if (len(duplicates) != 0): trial_counter += 1 print("Trial # {0}: {1} dates occur more than once!".format( repeats + 1, len(duplicates))) for x in range(len(duplicates)): print("({0} {1})".format( md.month_num_to_string(duplicates[x][0]), duplicates[x][1])) #convert dates as numbers to strings else:
bdays = int(input("How many birthdays should I generate per trial?\n>")) #accumulators trial = 1 counter = 0 #simulation for it in range(trials): #accumulator lst = [] # random birthday creation for i in range(bdays): lst.append(mydate.generate_date(0, 2020)) #removing years lst = mydate.remove_years(lst) #if there are duplicate birthdays if listutils.has_duplicates(lst) == True: #add one to counter counter += 1 #create a list of birthdays dupilcate = listutils.get_duplicates(lst) #start string dupilcates = "(" #type of string return varys depending on the amount of duplicates if len(dupilcate) > 1: #add to string
print() trials_with_dupe = 0 for i in range(num_trials): b_days = [] for j in range(num_bdays): date = mydate.generate_date(1960, 2017) b_days.append(date) b_day1 = mydate.remove_years(b_days) dupe = duplicates(b_day1) trial_summary = "" if len(dupe) == 0: trial_summary = "Trial #" + str(i + 1) + ": No dates are the same." else: trials_with_dupe += 1 trial_summary = "Trial #" + str(i + 1) + ": " + str(len(dupe)) + " date(s) occur more than once!"
import mydate import listutils simulation_repeats = int( input("How many times should I run the simultation?\n")) num_birthdays = int(input("How many birthdays should I generate per trial?\n")) trials_with_duplicates = 0 for i in range(0, simulation_repeats): duplicate_birthday_string = "" generated_dates = [] for j in range(0, num_birthdays): generated_dates.append(mydate.generate_date(1916, 2016)) mydate.remove_years(generated_dates) duplicate_birthdays = listutils.finding_duplicates(generated_dates) if duplicate_birthdays == []: print("Trial %i: No dates are the same" % (i + 1)) else: trials_with_duplicates += 1 for k in duplicate_birthdays: month = mydate.month_num_to_string(k[0]) if duplicate_birthdays.index(k) == (len(duplicate_birthdays) - 1): duplicate_birthday_string += month + " " + str(k[1]) else: duplicate_birthday_string += month + " " + str(k[1]) + ", " print("Trial %i: %i dates occur more than once! (%s)" % (i + 1, (len(duplicate_birthdays)), duplicate_birthday_string))