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)
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---