-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_in_words.py
72 lines (66 loc) · 1.67 KB
/
number_in_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from functools import cache
lookup_table = {
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'
}
@cache
def number_in_words(n: int) -> str:
"""
return the number in words. e.g. 342 -> three hundred and forty two
"""
if n <= 0:
return ''
elif n > 0 and n < 21:
return lookup_table[n]
elif n < 100:
ones = n % 10
tens = n - ones
if ones == 0:
return lookup_table[tens]
else:
return f'{lookup_table[tens]} {number_in_words(ones)}'
elif n < 1000:
hundreds = n // 100
balance = n % 100
if balance == 0:
return f'{number_in_words(hundreds)} hundred'
else:
return f'{number_in_words(hundreds)} hundred and {number_in_words(balance)}'
elif n < 1_000_000:
thousands = n // 1_000
balance = n % 1_000
if balance == 0:
return f'{number_in_words(thousands)} thousand'
elif balance < 100:
return f'{number_in_words(thousands)} thousand and {number_in_words(balance)}'
else:
return f'{number_in_words(thousands)} thousand {number_in_words(balance)}'
return ''
def count_letters(string: str) -> int:
return len(string.replace(' ', ''))