-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbest_sequence_3k.py
40 lines (31 loc) · 1.32 KB
/
best_sequence_3k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
def get_flips(num_flips=10):
"""Return a random sequence of coin flips."""
outcomes = ('H', 'T')
return random.choices(outcomes, k=num_flips)
def get_best_sequence(flips, threshold):
"""Find the sequence with the highest percentage
of heads, with a length above the threshold.
Returns:
- Sequence with highest heads percentage.
- Percentage heads for that sequence.
"""
best_percentage = -1.0
# Loop over all slices above the threshold size.
for start_index in range(len(flips)-threshold+1):
for end_index in range(start_index+threshold, len(flips)+1):
current_slice = flips[start_index:end_index]
percent_heads = current_slice.count("H") / len(current_slice)
# If this is the best sequence, store it.
if percent_heads > best_percentage:
best_percentage = percent_heads
best_sequence = current_slice
return best_sequence, best_percentage*100
# Generate flips.
all_flips = get_flips(num_flips=3_000)
print(f"Generated {len(all_flips):,} flips.")
# Find best sequence.
print("\nAnalyzing flips...")
best_sequence, percentage = get_best_sequence(all_flips, threshold=100)
print(f"\nThe best sequence has {len(best_sequence):,} flips in it.")
print(f"It consists of {percentage:.1f}% heads.")