Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for How Much Gold by ignalion
from fractions import Fraction
METALS = ('gold', 'tin', 'iron', 'copper')
def checkio(alloys):
"""
Find proportion of gold
"""
# According to description all test are solvable so let's find other 3 sums
for alloy in alloys.copy():
alloys['-'.join(set(METALS)-set(alloy.split('-')))] = \
1 - alloys[alloy]
# Now just calculate the proportion of gold from all three proportions that
# includes 'gold'
return (sum([alloys[x] for x in alloys if 'gold' in x]) - 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"
Feb. 19, 2014
Comments: