Skip to content

Commit

Permalink
Create Exercise 7: Flip the coin.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alkhdaniel authored Apr 15, 2022
1 parent 763f072 commit 032994b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions chapter-2/Exercise 7: Flip the coin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np

def generate(p1):
# change this so that it generates 10000 random zeros and ones
# where the probability of one is p1
return np.random.choice([0,1], p=[1-p1, p1], size=10000)

def count(seq):
result = 0
i = 0
for i in range(len(seq)-4): #iterate through all numbers in the sequence except the last 4 (since they cant have 5 1's in a row)
if seq[i:i+5].all(): #checks for a sequence of 5 1's in a row
result+=1; #add 1 to result
return result

def main(p1):
seq = generate(p1)
return count(seq)

print(main(2/3))

0 comments on commit 032994b

Please sign in to comment.