#Recursive definition. Return probability.
def Socks(p, u, f):
#No unoccupied spots remaining.
if u < 0:
prob = 0
#More unoccupied spots than pairs.
elif u >= p:
prob = 1
else:
#Fill a spot with one sock.
prob1 = 2*p / (2*p+f) * Socks(p-1, u-1, f+1)
#Get rid of a spot-filled sock with its pair.
prob2 = f / (2*p+f) * Socks(p, u+1, f-1)
prob = prob1 + prob2
return(prob)
Socks(14, 9, 0)
0.7004899349764068