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 eugene100372
from fractions import Fraction
METALS = ('gold', 'tin', 'iron', 'copper')
def det(M):
if len(M)==1: return M[0][0]
return sum(M[0][i]*(-1)**i*det([M[j][:i]+M[j][i+1:] for j in range(1,len(M))])
for i in range(len(M)))
def checkio(alloys):
L1,L2=[[1,1,1,1]],[[1,1,1,1]]
for key in alloys:
pair=key.split('-')
L1.append([Fraction(1*(METALS[i] in pair)) for i in range(4)])
l=list(L1[-1])
l[0]=alloys[key]
L2.append(l)
return det(L2)/det(L1)
#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"
July 16, 2019