0

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.

3
  • 5
    You should really learn about lists and dictionaries; 10 if statements at a time can be replaced by using a dictionary lookup, for example. Commented Apr 14, 2014 at 16:41
  • 3
    Think of what you could achieve with 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'}.... Commented Apr 14, 2014 at 16:42
  • 1
    Running the provided examples, no_to_words(342) == 'nine hundred and two' and no_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. Commented Apr 14, 2014 at 16:44

2 Answers 2

3

You never covered the case where tens==1. And you don't consider exact multiples of 100.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep. So there are missing "ten"s -- e.g. 210 becomes "two hundred and" -- and there are "and"s where there shouldn't be, e.g. 200 also becomes "two hundred and".
This was exactly what I needed ! Thanks for pointing those two points out.
1

It seems like you might be re-inventing the wheel a little bit here. Instead of using no_to_words I used the num2words library. That seemed to give me the right answer.

total = 0
for num in range(1, 1001):
    total += len(num2words(num).replace(" ", "").replace("-", ""))

print(total)
21124

2 Comments

I think I can see your perspective but if someone is interested in re-making the function again I would think num2words is the best code to model. If you don't want to look at the source code perhaps the unit tests would help you ferret out the problems. I think the best lesson this problem teaches people starting out in python is before you start a task that requires a significant amount of energy (and I would say 186 lines of code qualifies) look in package index to see if there is something you can leverage to do your task in a fraction of the time.
The only part I disagree w/ is that solving project Euler problems isn't about HAVING code to solve the problem, but figuring out HOW to write it. And relying on a library to do all of the interesting bits seems to go completely against that. In fact, if OP HADN'T mentioned Project Euler (or something like it), I'd be campaigning for your answer to be the accepted one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.