Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from copy import copy
seq_length = 20
def decimalToBinary(n):
# return padded binary number as list
binary_number_as_str=bin(n)
binary_number_as_str = binary_number_as_str[2:]
binary_number_as_str = binary_number_as_str.zfill(seq_length)
binary_number_as_list = [int(x) for x in binary_number_as_str]
return binary_number_as_list
def generateseq(float_number):
return decimalToBinary(float_number)
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(float_number, min_heads=5):
flips_data = generateseq(float_number)
sequences_found = find_sequences_in_data(flips_data)
longest_seq_of_heads = longest_sequence_of_heads(sequences_found)
if longest_seq_of_heads>=min_heads:
return 1
else:
return 0
count_min_heads=[]
for i in range(2**seq_length):
result = atleastnheadsinseq(i)
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
You can’t perform that action at this time.