1
+ # coin_flip_streaks.py
2
+
3
+ # A program that investigates how often a streak of N head or N tails
4
+ # comes up in a randomly generated list of heads and tails.
5
+
6
+ import random
7
+
8
+
9
+ def coin_flips (sample_size = 100 ):
10
+ """
11
+ Creates a list of a 'sample_size' number of 'H' and 'T's for heads and tails.
12
+
13
+ Parameters
14
+ ----------
15
+ sample_size : int, optional
16
+ The number of 'coin flips' to execute, by default 100
17
+
18
+ Returns
19
+ -------
20
+ results_list: list
21
+ A list of 'H' and 'T's depicting the results fron the coin flips.
22
+ """
23
+ SAMPLE_SIZE = sample_size
24
+ results_list = []
25
+
26
+ for experiment_num in range (SAMPLE_SIZE ): # Generate list of heads and tails values.
27
+ coin_flip = random .choice (['H' , 'T' ]) # Random 'H' or 'T'
28
+ results_list .append (coin_flip )
29
+
30
+ return results_list
31
+
32
+
33
+
34
+ def streak_tracker (flips_list = [], streak_of = 3 ):
35
+ """
36
+ Determines the number of streaks of a specific length, 'streak_of'.
37
+
38
+ Parameters
39
+ ----------
40
+ flips_list : list, optional
41
+ The list to search for streaks, by default []
42
+ streak_of : int, optional
43
+ The length necessary to count as a streak, by default 3
44
+
45
+ Returns
46
+ -------
47
+ dict
48
+ A dictionary of the form: {'HEAD STREAKS': ###, 'TAIL STREAKS': ###},
49
+ that displays how many of each type of streak occurred.
50
+ """
51
+ STREAKS = {'HEADS' : 0 , 'TAILS' : 0 } # Keep a dictionary to track counts.
52
+ HEAD_STREAKS , TAIL_STREAKS = (0 , 0 ) # Keep track of the type of streak and how many.
53
+
54
+ if (flips_list != []): # CASE: Nonempty list
55
+ previous = None
56
+ for i , current in enumerate (flips_list ):
57
+ # Catch streaks & Reset dictionary values
58
+ if (STREAKS ['HEADS' ] == streak_of ):
59
+ HEAD_STREAKS += 1
60
+ STREAKS ['HEADS' ] = 0
61
+ elif (STREAKS ['TAILS' ] == streak_of ):
62
+ TAIL_STREAKS += 1
63
+ STREAKS ['TAILS' ] = 0
64
+
65
+ # CASE: Same as previous value
66
+ if (current == previous ):
67
+ if (current == 'H' ):
68
+ STREAKS ['HEADS' ] += 1
69
+ elif (current == 'T' ):
70
+ STREAKS ['TAILS' ] += 1
71
+ # CASE: Different than previous value
72
+ else :
73
+ if (current == 'H' ):
74
+ STREAKS ['HEADS' ] = 1
75
+ elif (current == 'T' ):
76
+ STREAKS ['TAILS' ] = 1
77
+
78
+ previous = flips_list [i - 1 ] # Store current as previous for next iteration
79
+
80
+ return {'HEAD STREAKS' : HEAD_STREAKS , 'TAIL STREAKS' : TAIL_STREAKS }
81
+
82
+ else : # CASE: Empty list
83
+ return {'HEAD STREAKS' : None , 'TAIL STREAKS' : None }
84
+
85
+
86
+
87
+ # TESTS COMMENTED BELOW:
88
+ '''
89
+ coin_flip_results = coin_flips(10000)
90
+ print(' '.join(coin_flip_results))
91
+
92
+ sample = [ 'H', 'H', 'T', 'H', 'T', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'T',
93
+ 'H', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'H' ]
94
+ head_sample = ['H', 'H', 'H', 'H', 'H', 'H', 'H']
95
+ tail_sample = ['T', 'T', 'T', 'T', 'T', 'T', 'T']
96
+
97
+ report_back = streak_tracker(coin_flip_results, 7)
98
+ print(report_back)
99
+ '''
0 commit comments