Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:14:49+00:00 2026-06-10T11:14:49+00:00

I posted this question a few hours ago but I think I deleted it!

  • 0

I posted this question a few hours ago but I think I deleted it! Really sorry… I am working on Project Euler Problem 17.

Although there are other more obvious solutions, as a learning exercise, I approached the problem intending to solve it using recursion. I had also hoped that certain pieces of the code might later be usable in other contexts. The problem description itself is in the docstring at the top of the code, for those that are unfamiliar.

Here is the code in question:

"""If the numbers 1 to 5 are written out in words:
one, two, three, four, five

then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words,
how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two)
contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of
"and" when writing out numbers is in compliance with British usage.
"""

import collections
import re


SMALLS =       [(0,     "zero"),
                (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")]

MULTS_OF_TEN = [(20,    "twenty"),
                (30,    "thirty"),
                (40,    "forty"),
                (50,    "fifty"),
                (60,    "sixty"),
                (70,    "seventy"),
                (80,    "eighty"),
                (90,    "ninety")]

HUNDRED =      [(100,   "hundred")]

BIGS =         [(1000,  "thousand"),
                (10**6, "million"),
                (10**9, "billion")]

# other bigs: trillion, quadrillion, quintillion, sextillion, septillion, octillion, nonillion, decillion,
#             undecillion, duodecillion, tredecillion, quattuordecillion, quindecillion, sexdecillion,
#             septendecillion, octodecillion, novemdecillion, vigintillion

SMALLS = collections.OrderedDict(reversed(SMALLS))
MULTS_OF_TEN = collections.OrderedDict(reversed(MULTS_OF_TEN))
HUNDRED = collections.OrderedDict(reversed(HUNDRED))
BIGS = collections.OrderedDict(reversed(BIGS))


def int_to_words(num, follows_hundreds=False):
    """Retuns the text-equivelent of num, using recursion for distinct
    pieces.
    """
    def do_chunk(n,
                 num_text_map,
                 include_quotient=True,
                 is_hundreds=False):

        for x in num_text_map:
            quotient = n // x
            remainder = n % x

            if n == x: return num_text_map[x]

            if quotient and remainder:
                quotient_text = (int_to_words(quotient) + " " + num_text_map[x]) \
                                if include_quotient else \
                                (num_text_map[x])
                remainder_text = int_to_words(remainder, follows_hundreds=True) \
                                if is_hundreds else \
                                int_to_words(remainder)
                return quotient_text + " " + remainder_text

            elif quotient:
                quotient_text = (int_to_words(quotient) + " " + num_text_map[x]) \
                                if include_quotient else \
                                (num_text_map[x])
                return quotient_text

        return False

    result = do_chunk(num, BIGS)
    if result: return result

    result = do_chunk(num, HUNDRED, is_hundreds=True)
    if result: return result

    result = do_chunk(num, MULTS_OF_TEN, include_quotient=False)
    if result and follows_hundreds: return "and " + result
    if result: return result

    result = do_chunk(num, SMALLS)
    if result and follows_hundreds: return "and " + result
    if result: return result

def count_letters(string):
    looking_for = "[a-z]"
    instances = re.findall(looking_for, string)
    return len(instances)

def tally_letters(start, end):
    total_letters = 0
    for i in range(start, end + 1):
        total_letters += count_letters(int_to_words(i))
    return total_letters

And here is the output of the program, compared against the expected solution.

>>> answer = tally_letters(1, 1000)
>>> assert answer == 21124
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    assert answer == 21124
AssertionError
>>> answer
1: 21118

It confounds me that I’m off by a difference of 6. Thanks in advance for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:14:51+00:00Added an answer on June 10, 2026 at 11:14 am

    I think if someone gave me nine balloons, and then gave me one more, I would say that I had ten balloons. On the other hand, if I had ninety-nine balloons, and then I received one more, I would say “I have one hundred balloons”, not “I have hundred balloons”:

    >>> int_to_words(10)
    'ten'
    >>> int_to_words(100)
    'hundred'
    >>> int_to_words(1000)
    'thousand'
    

    len("one")*2 == 6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Posted the background to this question a few days ago.. but the answer
a few days ago I posted this question on the ANTLR mailinglist, but didn't
I posted about this a few weeks ago, but I don't think I asked
A few days ago I posted this question and everyone suggested me to use
I postet this question a few days ago but I didn't explain exactly what
I posted this question on superuser a bit ago, but I haven't gotten an
A few days ago, I posted this question and this question asking about how
This is a spin off from another question I posted a few days ago
I posted this question a few days ago, and I have some follow up
Last Updated: 2009-08-11 2:30pm EDT A few days ago I posted this question about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.