Sum letters in written numbers from 1 to 2008

This is a discussion on Sum letters in written numbers from 1 to 2008 within the C++ Programming forums, part of the General Programming Boards category; Hey there. Haven't been coding in a while, but I am doing challenges on DareYourMind.net and can't figure why I ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Sum letters in written numbers from 1 to 2008

    Hey there. Haven't been coding in a while, but I am doing challenges on DareYourMind.net and can't figure why I do not get the correct answer. I've picked quite a few numbers (~20-30) and calculated by hand the results and I get the same as the output from my program.

    Basically, I have to input the total number of letters of all the written numbers from 1 to 2008.

    Here's what I have coded, I commented it as much as possible for you guys:
    Code:
    #include <iostream>
    
    int main()
    {
        int total = 0, current = 0, temp = 0;
    
        for(int i = 1; i < 2009; i++)
        {
            current = 0;
    
            // if Xxxx is non-zero, then must add "one/two thousand"
            if(i / 1000) current += 11;
    
            // if xXxx is non-zero, then must add "one...nine"
            // and if xxXX is non-zero, then must add "hundred and"
            // otherwise add "hundred"
            if(i / 100 && ((i / 100) % 10))
            {
                if(i % 100 == 0) current += 7; // hundred
                else current += 10; // hundred and
    
                temp = (i / 100) % 10;
                switch(temp)
                {
                    case 1: case 2: case 6: current += 3; break;
                    case 3: case 7: case 8: current += 5; break;
                    case 4: case 5: case 9: current += 4; break;
                }
            }
    
            // if xxXx is non-zero, then add "ten...ninety"
            // separate case for numbers between 11 and 19 for "eleven...nineteen"
            if((i / 10) && ((i / 10) % 10))
            {
                temp = i % 100;
                if(temp > 10 && temp < 20)
                {
                    switch(temp)
                    {
                        case 11: case 12: current += 6; break;
                        case 16: case 15: current += 7; break;
                        case 17: current += 9; break;
                        case 13: case 18: case 14: case 19: current += 8; break;
                    }
                }
                else
                {
                    temp = (i / 10) % 10;
                    switch(temp)
                    {
                        case 1: current += 3; break;
                        case 2: case 3: case 4: case 8: case 9: current += 6; break;
                        case 5: case 6: current += 5; break;
                        case 7: current += 7; break;
                    }
                }
            }
    
            // make sure we're not in the "eleven...nineteen" range
            // and xxxX is non-zero, then add "one...nine"
            if((((i % 100) < 10) || ((i % 100) > 20)) && !(i % 10))
            {
                temp = i % 10;
                switch(temp)
                {
                    case 1: case 2: case 6: current += 3; break;
                    case 3: case 7: case 8: current += 5; break;
                    case 4: case 5: case 9: current += 4; break;
                }
            }
            std::cout << i << " : " << current << "\t";
            total += current;
        }
        std::cout << std::endl << total;
    }
    Any help is much welcome !

    Thanks (:

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    If you get the same result by hand as you get from your program, what's the problem?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm sorry I wasn't clear. I have tested my answer on the site's challenge verification page and it does not validate hence I have to be wrong somewhere but I have looked at it and can't find my mistake =/

    I can link to the challenge if needed.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Desolation View Post

    I can link to the challenge if needed.
    That would be helpful.

    edit: You seem to be doing nothing for the numbers 1-9, or for the units column in numbers > 20.
    Do blank spaces count?
    Is it necessarily "hundred and"? (I, for example, would say "one hundred five" or "one hundred twenty four", not "one hundred and ...")
    Last edited by R.Stiltskin; 04-26-2010 at 01:54 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Here's the link to the challenge:
    Hacker-Challenge | Dare Your Mind :: Challenges :: ONE plus TWO is 6

    As for your comment, I will check this right now, thanks.

    Edit: Only letters count (dashes and spaces don't).

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    I will try without "hundred and". English is not my native language so I thought the "and" was compulsory.

    As for your first suggestion, it is dealt with in this part of the code:
    Code:
            // make sure we're not in the "eleven...nineteen" range
            // and xxxX is non-zero, then add "one...nine"
            if(((i % 100) < 10) || ((i % 100) > 20)))
            {
                temp = i % 10;
                switch(temp)
                {
                    case 1: case 2: case 6: current += 3; break;
                    case 3: case 7: case 8: current += 5; break;
                    case 4: case 5: case 9: current += 4; break;
                }
            }
    Basically it extracts the last two digits on the right and check wether they are between 10 and 20. If it is not and the last digit is non-zero then we add the number of letters contained in each written number.

    I have double-checked all hard-coded values (i.e. "magic numbers").

    Edit: Removed that last bit because it produced weird results. Sorry about that.

    Edit: I have tried without "hundred and" and I still get the wrong output.
    Last edited by Desolation; 04-26-2010 at 02:04 PM.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I looked at the challenge and they seem to want "hundred and ..." so that part's OK. But in the code you posted in #1, for the last if statement you have
    Code:
        if ((((i % 100) < 10) || ((i % 100) > 20)) && !(i % 10))
    That last term should not have "!".

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I have rewritten it from scratch so that it is easier to spot mistakes. However, I get the exact same result and this time I'm pretty sure there is no possible mistake as the output seems correct to me. Here's the new code:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string thousand = "thousand";
        std::string hundreds[] = {"hundred", "hundredand"};
        std::string tens[] = {"ten", "twenty", "thirty", "fourty", "fifty",
                            "sixty", "seventy", "eighty", "ninety"};
        std::string eton[] = {"eleven", "twelve", "thirteen", "fourteen",
                        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        std::string units[] = {"one", "two", "three", "four", "five", "six",
                        "seven", "eight", "nine"};
        
        int total = 0;
        std::string tmp = "";
        for(int i = 1; i < 2009; i++)
        {
            tmp = "";
            if(i / 1000)
            {
                if((i/1000)%10 == 1) tmp += units[0];
                else tmp += units[1];
                tmp += thousand;
            }
            
            if(i / 100 && ((i/100)%10))
            {
                tmp += units[((i/100) % 10)-1];
                if(i%100) tmp += hundreds[1];
                else tmp += hundreds[0];
            }
            
            if(i / 10 && ((i/10) %10))
            {
                if((i % 100) <= 10 || (i % 100) >= 20)
                    tmp += tens[((i / 10) % 10)-1];
                else
                    tmp += eton[(i%10)-1];
            }
            if(i % 10 && ((i % 100) <= 10 || (i % 100) >= 20))
                tmp += units[(i%10)-1];
            
            total += tmp.size();
            std::cout << tmp << " : " << tmp.size() << "\n";
        }
        std::cout << std::endl << total;
    }
    And I have posted the output on here because it is pretty large.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I've noticed there is no defined output for the challenge, so you may just be getting marked unfairly.

    Try the simplest output:
    Code:
    std::cout << tmp.size() << std::endl;
    I also think you misunderstand the goal. Aren't you supposed to sum the lengths of every written number and get one giant number?

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    The challenge does not validate code, it valides user input (i.e. you need to type in the answer). This number is summed in the "total" variable which is done right above the line you quoted and is outputted after the loop.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    I made a small change to the output so that the digit representation is shown as well.

    new output | Copy Paste Code

    Edit: I have corrected a spelling mistake ("fourty" instead of "forty") but I still get the wrong output. Meh.
    Last edited by Desolation; 04-26-2010 at 03:12 PM.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I'm saying that the judge will only look at a number and if you output too much you will be marked wrong. So make your only output the plain answer. If that doesn't work we know something else is wrong.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    903
    No offense, but have you actually looked at the link I have posted ?

    The output is not judged. The sum of all written numbers is what is judged and it is the only information available to the verification process on the website. They will never ask for my code, for an executable file or for a specific output layout. It is a plain textbox which requires keyboard input (the sum of all written numbers).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 02:43 PM
  2. Maxium sum in a subvecter
    By sniperwire in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 07:33 PM
  3. Convert Phone Letters To Numbers
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 04:53 PM
  4. Transforming letters to numbers...
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 03:26 PM
  5. How do you allocate numbers to letters?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 06:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21