(July 4, 2016 at 4:47 am)SteelCurtain Wrote: Problem #17 was easy in theory, required a lot of debugging...
Code:def main():
tot = 0
for i in range(1, 1001):
n = str(i)
if len(n) == 1: #one digit
tot += ones('0', n[0])
elif len(n) == 2: #two digits
tot += tens(n[0], n[1]) + ones(n[0], n[1])
elif len(str(i)) == 3: #three digits
tot += hundreds(n[0], n[1], n[2]) + tens(n[1], n[2]) + ones(n[1], n[2])
else: #add letters for 'one thousand'
tot += 11
print(tot)
def ones(tens, ones):
if tens == '1':
return 0
else:
if any(ones == x for x in ['1', '2', '6']):
return 3
elif any(ones == x for x in ['4', '5', '9']):
return 4
elif any(ones == x for x in ['3', '7', '8']):
return 5
else:
return 0
def tens(tens, ones):
if tens == '1':
if any(ones == x for x in ['1', '2']):
return 6
elif any(ones == x for x in ['5', '6']):
return 7
elif any(ones == x for x in ['3', '4', '8', '9']):
return 8
elif ones == '7':
return 9
elif ones == '0':
return 3
elif any(tens == x for x in ['2', '3', '8', '9']):
return 6
elif any(tens == x for x in ['4', '5', '6']):
return 5
elif tens == '7':
return 7
else:
return 0
def hundreds(hunds, tns, ons):
if (tns == '0' and ons == '0'):
return 7 + ones('0', hunds)
else:
return 10 + ones('0', hunds)
If you want to see my code, go to Page 8 in the corresponding thread and it should be the 15th post. Country is Australia, I'm right after the Chinese dude.