Solve problem 121 in Python and update template creation lib
This commit is contained in:
45
python/e121.py
Normal file
45
python/e121.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from fractions import Fraction
|
||||||
|
from itertools import combinations
|
||||||
|
|
||||||
|
|
||||||
|
def mul(xs):
|
||||||
|
r = 1
|
||||||
|
for x in xs:
|
||||||
|
r *= x
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def mul_inv(xs):
|
||||||
|
r = 1
|
||||||
|
for x in xs:
|
||||||
|
r *= (1 - x)
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def sub_odds(n_blue, n_total):
|
||||||
|
""" Computes the odds for getting exactly n_blue out of n_total blue discs.
|
||||||
|
"""
|
||||||
|
total_odds = 0
|
||||||
|
odds = [Fraction(1, n) for n in range(2, 2 + n_total)]
|
||||||
|
odds_set = set(odds)
|
||||||
|
for odd in combinations(odds, n_blue):
|
||||||
|
odd_inv = tuple(odds_set.difference(set(odd)))
|
||||||
|
total_odds += mul(odd) * mul_inv(odd_inv)
|
||||||
|
return total_odds
|
||||||
|
|
||||||
|
|
||||||
|
def euler_121():
|
||||||
|
n_turns = 15
|
||||||
|
n_to_win = n_turns // 2 + 1
|
||||||
|
odds = 0
|
||||||
|
for n_blue in range(n_to_win, n_turns + 1):
|
||||||
|
odds += sub_odds(n_blue, n_turns)
|
||||||
|
price = int(1 / odds)
|
||||||
|
return price
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = euler_121()
|
||||||
|
print("e121.py: " + str(solution))
|
||||||
|
assert(solution == 2269)
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
FROM = 206
|
FROM = 100
|
||||||
TILL = 206
|
TILL = 100
|
||||||
|
|
||||||
template = """
|
TEMPLATE = """
|
||||||
def euler_XXX():
|
def euler_XXX():
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@@ -9,7 +9,8 @@ def euler_XXX():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
solution = euler_XXX()
|
solution = euler_XXX()
|
||||||
print("eXXX.py: " + str(solution))
|
print("eXXX.py: " + str(solution))
|
||||||
assert(solution == 0)
|
# assert(solution == 0)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@@ -20,6 +21,6 @@ for i in range(FROM, TILL + 1):
|
|||||||
e = str(i)
|
e = str(i)
|
||||||
filename = "e" + e + ".py"
|
filename = "e" + e + ".py"
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
s = template.replace("XXX", e)
|
s = TEMPLATE.replace("XXX", e)
|
||||||
f.write(s)
|
f.write(s)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user