Last active
January 21, 2022 00:35
-
-
Save robcarver17/752ef3b7d8b416406620a22811564074 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from copy import copy | |
def ishead(): | |
result=random.uniform(0,1) | |
if result>=0.5: | |
return 1 | |
else: | |
return 0 | |
def generateseq(seq_length=20): | |
seq_of_flips=[] | |
for i in range(seq_length): | |
seq_of_flips.append(ishead()) | |
return seq_of_flips | |
def find_sequences_in_data(data): | |
""" | |
Find the sequences in a list of data | |
:return: list of sequences | |
""" | |
data_left = copy(data) | |
sequences_found = [] | |
while len(data_left)>0: | |
if len(data_left)==1: | |
sequences_found.append([data_left[0]]) | |
return sequences_found | |
offset=0 | |
in_sequence = True | |
this_sequence = [] | |
while in_sequence: | |
this_sequence.append(data_left[offset]) | |
if offset==(len(data_left)-1): | |
# end of the line | |
break | |
if data_left[offset]!=data_left[offset+1]: | |
# out of sequence | |
break | |
offset = offset+1 | |
sequences_found.append(this_sequence) | |
data_left = data_left[offset+1:] | |
return sequences_found | |
def longest_sequence_of_heads(sequences_found): | |
longest_length = 0 | |
for seq in sequences_found: | |
if all(x == 1 for x in seq): | |
# all heads | |
len_heads = len(seq) | |
if len_heads>longest_length: | |
longest_length = len_heads | |
return longest_length | |
def atleastnheadsinseq(min_heads=5, seq_length=20): | |
flips_data = generateseq(seq_length) | |
sequences_found = find_sequences_in_data(flips_data) | |
length_of_longest_run_of_heads = longest_sequence_of_heads(sequences_found) | |
if length_of_longest_run_of_heads>=min_heads: | |
return 1 | |
else: | |
return 0 | |
count_min_heads=[] | |
# start with small values to begin with | |
for i in range(1000000): | |
result = atleastnheadsinseq() | |
count_min_heads.append(result) | |
print(sum(count_min_heads)/len(count_min_heads)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment