I apologize beforehand if the question has been repeated so many times. This is a practice task from Automate the Boring Stuff with Python. In brief, the task entails writing a code that carries out an experiment of checking if there is a streak of 6 'heads' or 'tails' in 100 coin tosses, then replicates it 10,000 times and gives a percentage of the success rate.
When I wrote the code, I tried to be different by making the code applicable to any streaks in a number of predetermined experiments (in my case, the sample was 1 million coin tosses). I also tried to find the longest streak possible in that said experiment.
I also want to apologize beforehand that the comments were awfully verbose.
import random, copy, time def torai(seq,pop): # seq is for #=streak, pop is for total sample/population/experiment # Creating a random chance of heads and tails tosses =  for i in range(pop): tosses.append(random.randint(1,2)) # 1 and 2 for head and tail, and vice versa # Defining initial values for the main loop streak = 0 # Iterated streak curlongstr = 0 # Current longest streak longeststr = 0 # Longest streak evaluated peak =  # Record local streaks from 'tosses' list # The main loop for i in range(len(tosses)): # Looping based on list indexes if i == 0: # Conditional for preventing tosses == tosses[-1] continue elif tosses[i] == tosses[i-1]: # Conditional for checking if an i element has the same value as the previous element value, i-1 streak += 1 # Adding tally mark if the line above is fulfilled if i == len(tosses)-1: # A nested conditional for adding the last tally mark from 'tosses' into the overall list of steaks 'peak', see lines 27-33 peak.append(streak) elif tosses[i] != tosses[i-1]: # Conditional for checking if an i element value is different than the previous element value, i-1 curlongstr = copy.copy(streak) # Creating a variable by returning a copy of streak before it resets to 0, see line 31 if curlongstr > longeststr: # A nested conditional for comparing the current longest streak and the longest streak that has happened when looping the 'tosses' list longeststr = curlongstr streak = 0 # This is where streaks ended and then resets to 0, so before that, the value of the streak is copied first, see line 28 if curlongstr > streak: # After streak is reset to 0, the value of current long streak is compared to 0, so that we create a list of streaks from 'tosses' list peak.append(curlongstr) truepeak =  for i in peak: # Example: a 2-streak is equal to either [1,1,1] or [2,2,2], a 4-streak is either [1,1,1,1,1] or [2,2,2,2,2] truepeak.append(i+1) apr =  # Loop for finding how many #-streaks happened for i in truepeak: if i == seq: apr.append(i) print('%s-streak count: ' %seq, len(apr)) # Total of #-streaks happened in 'tosses' list print('%s-streak prob (percent): ' %seq, (len(apr)/pop)*100) # Calculating probability if how many #-streak happened in given n times tosses print('longest streak: ',longeststr + 1) # Similar reason as line 36 print('process time: ',time.process_time(), 'second\n') return (len(apr)/pop)*100 x = torai(2,1000000) y = torai(6,1000000) z = torai(10,1000000) print(x, y, z)
I tried to increase the sample to 10 million coin tosses. However, the program will run 9-10 slower each time the function was called.
My request are can anyone check whether if the result (probability of n-streak) is right or not and are there any ways to make the code and process time shorter?