Skip to content

Instantly share code, notes, and snippets.

@luccabb
Created April 5, 2020 19:15
Show Gist options
  • Save luccabb/82bb9bc15232ed3bb32a3953573c5e5e to your computer and use it in GitHub Desktop.
Save luccabb/82bb9bc15232ed3bb32a3953573c5e5e to your computer and use it in GitHub Desktop.
import random
import matplotlib.pyplot as plt
def hatCheck(n):
# using index as the id for people
# creating list for hats from 0 to 'n' matching
# the index and then shuffling it
hat = [a for a in range(0,n)]
random.shuffle(hat)
correct =0
for a in range(len(hat)):
# checking for every element if the index matches the number in that index
# which would be the case of delivering the hat for the right person
if a == hat[a]:
correct += 1
return correct
def average(start, end, n):
# returning an array averaging 'n' times the output
# from hatCheck from 'start' to 'end'
# for a in range (start, end)
# it will run hatCheck for all those numbers 'n' times
# them it will sum this whole list that comes out of hatCheck
# and divide it by the number of elements in it 'n' to get the average
# and it will put this average in the final list
# it will do this process for every element between 'start' and 'end'
return [(sum([hatCheck(a) for _ in range(n)])/n) for a in range(start, end)]
assert hatCheck(1) == 1
assert hatCheck(2) <= 10
# Graphing average of hats delivered correctly given the number
# of people
graph = average(1,50,10000)
plt.plot([a for a in range(49)], graph)
plt.title('Number of people X number of hats delivered correctly')
plt.xlabel('Number of people')
plt.ylabel('Average number of hats delivered correctly')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment