Project Overview:
See how many times numbers would repeat x times on a digital clock output. This particular code only counts from 12:00 to 11:59, so it's technically only half of a day, I could take the (output x 2) for a full day.
Functions:
input_io(): Input the number of times you would like to see if a number repeats (for example 11:11 will have the number "1" repeated four times. 12:11 would have 3 repeated "1"s, etc.)
gen_times(): Generate a list of all the "digital clock" outputs starting from 12:00 to 11:59 - including leading zeros for minutes (e.g. 12:04 would need the zero before the "4").
count_repeat(): Test each "clock output" (from gen_times()) to see how many times the numbers 0-9 repeat in each one. If it is greater than the input time add one to the counter.
Questions:
In the code, you'll see that I return values from functions and immediately assign them to variables. I'm not sure the proper conventions of this in Python. Are there ways to pass-through variables from function to function - is it reasonable to assign variables outside of functions to function outputs?
Is there a better way to run this program without all of the coercion? The main use case of the coercion is for the leading zeros when generating times.
Code:
def input_io(): input_repeats = input("Please input the amount of times you'd like to see if numbers repeat") while True: try: input_repeats = int(input_repeats) break except: print("Please enter an integer.") input_repeats = input() return input_repeats input_repeats = input_io() def gen_times(): total_minutes = (24 * 60) / 2 print(f"Total Minutes {total_minutes}") times = ["0"] * int(total_minutes) hours = "12" minutes = "00" i = 0 while (i < total_minutes): if(hours==13): hours=1 times[i] = str(hours)+str(minutes) #print(int(minutes)) if(minutes == "59"): minutes = "00" hours = int(hours) + 1 #ADDING "0" IN FRONT OF HOURS #if(int(hours) >= 10): # hours = hours #else: # hours = "0"+str(hours) else: minutes = str(int(minutes)+1) if (int(minutes) >= 10): minutes = minutes else: minutes = "0" + str(minutes) i += 1 return times times = gen_times() print(times) def count_repeat(): counter_repeat = 0 i=0 ii=0 iterationcounter = 0 while(i != len(times)): ii=0 while(ii < 10): count = times[i].count(str(ii)) #iterationcounter+=1 if count >= int(input_repeats): counter_repeat+=1 break ii+=1 i+=1 return counter_repeat counter_repeat = count_repeat() print(f"Number of Times w/ {input_repeats} repeats: {counter_repeat}") print(counter_repeat,"/",len(times)) percent_repeat = (counter_repeat/len(times)) print("{:.2%}".format(percent_repeat)) #print(iterationcounter)