Counting letters in a number between 1 and 1000

  • Thread starter Thread starter ergospherical
  • Start date Start date
  • Tags Tags
    Counting
Click For Summary

Discussion Overview

The discussion revolves around counting the number of letters in the English representation of numbers between 1 and 1000, focusing on algorithm efficiency and code optimization. Participants explore various coding strategies and potential improvements to the initial algorithm.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents an initial algorithm to count letters in numbers, detailing specific lengths for digits, tens, and teens.
  • Another participant questions whether the algorithm accounts for the word "hundred" in its calculations.
  • Some participants suggest code optimizations, such as using `elif` statements and returning values directly instead of using an intermediate count.
  • Concerns are raised about the robustness of the algorithm when faced with inputs outside the specified range, such as 1001 or 10000.
  • One participant reflects on the complexity of implementing a recursive solution versus a case-based approach.
  • There are discussions about code readability and the importance of comments for clarity.
  • Participants note potential issues with the condition checking for the value of n when it equals 1000.
  • Some participants express frustration with their own coding habits and the need for cleaner code formatting.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to optimize the algorithm, and there is no consensus on the most effective method or the handling of edge cases.

Contextual Notes

Limitations include the algorithm's handling of inputs outside the specified range and the dependency on specific conditions for counting letters, which may not be universally applicable.

ergospherical
Science Advisor
Homework Helper
Education Advisor
Insights Author
Messages
1,100
Reaction score
1,387
Excluding spaces and hyphens, how many letters are there in a given number between 1 and 1000 (inclusive)? How can I make my algorithm more efficient?

Python:
dig_lens = [4,3,3,5,4,4,3,5,5,4]
tens_lens = [0,3,6,6,6,5,5,7,6,6]
teens_lens = [0,6,6,8,8,7,7,9,8,8]

def num_letters(n):
    nlist = []
    while(n>=1):
        nlist.append(n%10)
        n//=10
    length = len(nlist)

    if(n==1000):
        return 11

    count = 0
    if(length == 1):
        count = count + dig_lens[n]

    if(length == 2):
        if(nlist[1]==1 and nlist[0]>0):
            count = count + teens_lens[nlist[0]]
        else:
            count = count + tens_lens[nlist[1]] + dig_lens[nlist[0]]

    if(length == 3):
        count = count + dig_lens[nlist[2]] + 7
        if(nlist[1]==0 and nlist[0]>0):
            count = count + 3 + dig_lens[nlist[0]]
        elif(nlist[1]==1 and nlist[0]>0):
            count = 3 + count + teens_lens[nlist[0]]
        else:
            count = 3 + count + tens_lens[nlist[1]] + dig_lens[nlist[0]]

    return count

##e.g.##
print(num_letters(816))
 
  • Like
Likes   Reactions: berkeman
Technology news on Phys.org
Does it ever pronounce "hundred"?
 
it does pronounce hundred - that's accounted for in the first line of the length=3 case!
 
Last edited:
OK, I see what you're doing. Hundred is "+7".
Eleven is "6".
 
  • Like
Likes   Reactions: ergospherical
If length == 1 then you do not need to test if length == 2 (use elif) Edit: or if you are done then return the answer.

You could use count += instead of count = count + ... but if count is 0 then you don't need to include it in any sums anyway. Edit: you hardly need an intermediate count, if you know the answer just return it.

Edit: use more spaces and use them consistently

Python:
dig_lens = [4, 3, 3, 5, 4, 4, 3, 5, 5, 4]
tens_lens = [0, 3, 6, 6, 6, 5, 5, 7, 6, 6]
teens_lens = [0, 6, 6, 8, 8, 7, 7, 9, 8, 8]def num_letters(n):
    nlist = []
    while n >= 1:
        nlist.append(n % 10)
        n //= 10
    length = len(nlist)

    if n == 1000:
        return 11

    if length == 1:
        return dig_lens[n]

    if length == 2:
        if nlist[1] == 1 and nlist[0] > 0:
            return teens_lens[nlist[0]]
        else:
            return tens_lens[nlist[1]] + dig_lens[nlist[0]]

    if length == 3:
        count = dig_lens[nlist[2]] + 7
        if nlist[1] == 0 and nlist[0] > 0:
            return count + 3 + dig_lens[nlist[0]]
        if nlist[1] == 1 and nlist[0] > 0:
            return count + 3 + teens_lens[nlist[0]]
        else:
            return count + 3 + tens_lens[nlist[1]] + dig_lens[nlist[0]]

    raise RuntimeError("Can't handle more than 1,000")##e.g.##
print(num_letters(10000))
 
Last edited:
  • Like
Likes   Reactions: ergospherical
Notice that the size of the number is an input, which means a user might not cooperate with the constraints. How robust is your program if the input parameter were 1,001? Or 10,000?

See how flexible and universal and bulletproof you can make it.
 
  • Like
Likes   Reactions: ergospherical
pbuk said:
Edit: you hardly need an intermediate count, if you know the answer just return it.
Yeah, I started thinking I was going to try and do some clever recursive algorithm, but realized it'd just be easier to split it into cases - but didn't get rid of the intermediate count.
 
Sorry, I have edited and re-edited my code, bad habit. It is prettier now. Should have just passed it through the Black parser straight off, my typing is a bit haphazard after a Friday night in the pub for the first time in 18 months.
 
Last edited:
  • Haha
Likes   Reactions: Filip Larsen and sysprog
Comments are good, for those who are trying to review your code.
I had a bunch of false starts before I got a handle on what it was doing.
 
  • Like
Likes   Reactions: pbuk
  • #10
pbuk said:
if n == 1000:
return 11
Looks like that test should use the original value of n to trigger properly?
 
  • #11
Filip Larsen said:
Looks like that test should use the original value of n to trigger properly?
whoopsie, yeah that and the length=1 case ought to be before the loop
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K