Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for How Much Gold by shota243
from fractions import Fraction
METALS = ('gold', 'tin', 'iron', 'copper')
METALSET = set(METALS)
def setstr(s):
return '-'.join(sorted(list(s)))
def checkio(alloys):
normalized = {}
for a, p in alloys.items():
m1, m2 = a.split('-')
normalized[setstr({m1, m2})] = p
normalized[setstr(METALSET - {m1, m2})] = 1 - p
return (normalized[setstr({'gold', 'iron'})] + normalized[setstr({'gold', 'tin'})] + normalized[setstr({'gold', 'copper'})] - 1) / 2
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio({
'gold-tin': Fraction(1, 2),
'gold-iron': Fraction(1, 3),
'gold-copper': Fraction(1, 4),
}) == Fraction(1, 24), "1/24 of gold"
assert checkio({
'tin-iron': Fraction(1, 2),
'iron-copper': Fraction(1, 2),
'copper-tin': Fraction(1, 2),
}) == Fraction(1, 4), "quarter"
March 23, 2014