Where Will the Sorting Hat Put You?
This Week's Fiddler
Consider the chance that a given person gets Graphindor, . SotThe next person's chance of getting Graphindor, assuming they have a 25% chance of picking any house, is (the chance the next person wants Graphindor and the current person did not pick Graphindor) plus (the chance the next person wants not Graphindor but whose choice matches the current person's), which equals .
We can start on and iterate on this process 8 times to find the chance the person immediately before us gets Graphindor. From then, we have a chance of getting Graphindor as we will always try to pick Graphindor.
from fractions import Fraction
n = 1
g = Fraction(1)
for n in range(2, 10):
n += 1
g = (1 - g) / 3
print(1 - g, float(1 - g))
1640/2187 0.7498856881572931
Thus, our chance is , or about .
Answer: 1640/2187 (about 74.9885688%)
Extra Credit
Consider that the only depending factor to the probability is how far away the person yelling "Graphindor" is from you. Being the person right in front of you is equivalent to calculating , being the person before that is equivalent to , etc. So at any , we need to calculate the probabilities from to and average them. The first point where the average exceeds is our answer.
from fractions import Fraction
n = 1
g = Fraction(1)
total = Fraction(0)
p = None
while True:
n += 1
g = (1 - g) / 3
total += (1 - g)
if n == 9:
p = 1 - g
print(p, float(p))
if p is not None and (total / n) > p:
print(n + 1, float(total / n))
break
1640/2187 0.7498856881572931
4922 0.7498856939646413
So is the first value of greater than 10 where our odds of getting Graphindor is better than . Our odds lie at about , exceeding .
Answer: 4922