I'm currently solving Project Euler's problem number 17 using Python. Here's the link for the problem statement:
http://projecteuler.net/problem=17
Here's my solution in Python:
def no_to_words(n):
num = str(n)
s = ""
hunds = n/100
ten = n%100
tens = ten/10
units = ten%10
if(len(num) == 3):
if(n == 100):
return "one hundred"
if(hunds == 1):
s = s + "one hundred and"
elif(hunds == 2):
s = s + "two hundred and"
elif(hunds == 3):
s = s + "three hundred and"
elif(hunds == 4):
s = s + "four hundred and"
elif(hunds == 5):
s = s + "five hundred and"
elif(hunds == 6):
s = s + "six hundred and"
elif(hunds == 7):
s = s + "seven hundred and"
elif(hunds == 8):
s = s + "eight hundred and"
else:
s = s + "nine hundred and"
if(ten == 11):
s = s + " eleven"
return s
elif(ten == 12):
s = s + " twelve"
return s
elif(ten == 13):
s = s + " thirteen"
return s
elif(ten == 14):
s = s + " fourteen"
return s
elif(ten == 15):
s = s + " fifteen"
return s
elif(ten == 16):
s = s + " sixteen"
return s
elif(ten == 17):
s = s + " seventeen"
return s
elif(ten == 18):
s = s + " eighteen"
return s
elif(ten == 19):
s = s + " nineteen"
return s
if(tens == 2):
s = s + " twenty"
elif(tens == 3):
s = s + " thirty"
elif(tens == 4):
s = s + " forty"
elif(tens == 5):
s = s + " fifty"
elif(tens == 6):
s = s + " sixty"
elif(tens == 7):
s = s + " seventy"
elif(tens == 8):
s = s + " eighty"
elif(tens == 9):
s = s + " ninety"
if(units == 1):
s = s + " one"
elif(units == 2):
s = s + " two"
elif(units == 3):
s = s + " three"
elif(units == 4):
s = s + " four"
elif(units == 5):
s = s + " five"
elif(units == 6):
s = s + " six"
elif(units == 7):
s = s + " seven"
elif(units == 8):
s = s + " eight"
elif(units == 9):
s = s + " nine"
if(len(num) == 2):
if(n == 10):
return "ten"
if(ten == 11):
s = s + "eleven"
return s
elif(ten == 12):
s = s + "twelve"
return s
elif(ten == 13):
s = s + "thirteen"
return s
elif(ten == 14):
s = s + "fourteen"
return s
elif(ten == 15):
s = s + "fifteen"
return s
elif(ten == 16):
s = s + "sixteen"
return s
elif(ten == 17):
s = s + "seventeen"
return s
elif(ten == 18):
s = s + "eighteen"
return s
elif(ten == 19):
s = s + "nineteen"
return s
if(tens == 2):
s = s + "twenty"
elif(tens == 3):
s = s + "thirty"
elif(tens == 4):
s = s + "forty"
elif(tens == 5):
s = s + "fifty"
elif(tens == 6):
s = s + "sixty"
elif(tens == 7):
s = s + "seventy"
elif(tens == 8):
s = s + "eighty"
elif(tens == 9):
s = s + "ninety"
if(units == 1):
s = s + " one"
elif(units == 2):
s = s + " two"
elif(units == 3):
s = s + " three"
elif(units == 4):
s = s + " four"
elif(units == 5):
s = s + " five"
elif(units == 6):
s = s + " six"
elif(units == 7):
s = s + " seven"
elif(units == 8):
s = s + " eight"
elif(units == 9):
s = s + " nine"
if(len(num) == 1):
if(units == 1):
s = "one"
elif(units == 2):
s = "two"
elif(units == 3):
s = "three"
elif(units == 4):
s = "four"
elif(units == 5):
s = "five"
elif(units == 6):
s = "six"
elif(units == 7):
s = "seven"
elif(units == 8):
s = "eight"
elif(units == 9):
s = "nine"
if(len(num) == 4):
return "one thousand"
return s
final = ""
for e in range(1, 1001):
s = no_to_words(e)
final = final + s
ultimate = final.replace(" ", "")
print len(ultimate)
Now, the answer for this problem is (spoiler):
21124
whereas, I'm getting 21121 as my answer. What's wrong with my code? I can't seem to figure out the problem.
number = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety',100:'hundred',1000:'thousand'}....no_to_words(342) == 'nine hundred and two'andno_to_words(115) == 'nine hundred and fifteen'. And I get the answer 16,981 from your code, so I don't know what you're running.