-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordifynum.py
77 lines (64 loc) · 2.08 KB
/
wordifynum.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
73
74
75
76
77
ones = {
0: '', 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'}
tens = {
2: 'twenty', 3: 'thirty', 4: 'forty', 5: 'fifty', 6: 'sixty',
7: 'seventy', 8: 'eighty', 9: 'ninety'}
illions = {
1: 'thousand', 2: 'million', 3: 'billion', 4: 'trillion', 5: 'quadrillion',
6: 'quintillion', 7: 'sextillion', 8: 'septillion', 9: 'octillion',
10: 'nonillion', 11: 'decillion'}
def say_number(i):
i = int(i)
"""
Convert an integer in to it's word representation.
say_number(i: integer) -> string
"""
if i < 0:
return _join('negative', _say_number_pos(-i))
if i == 0:
return 'zero'
return _say_number_pos(i)
def _say_number_pos(i):
if i < 20:
return ones[i]
if i < 100:
return _join(tens[i // 10], ones[i % 10])
if i < 1000:
return _divide(i, 100, 'hundred')
for illions_number, illions_name in illions.items():
if i < 1000**(illions_number + 1):
break
return _divide(i, 1000**illions_number, illions_name)
def say_ipnumber(i):
i = int(i)
"""
Convert an integer in to it's word representation.
say_number(i: integer) -> string
"""
if i < 0:
return _join('negative', _say_number_pos(-i))
if i == 0:
return 'zero'
return _say_ipnumber_pos(i)
def _say_ipnumber_pos(i):
if i < 20:
return ones[i]
if i < 100:
return _join(tens[i // 10], ones[i % 10])
if i < 1000:
return _divide(i, 100, '')
for illions_number, illions_name in illions.items():
if i < 1000**(illions_number + 1):
break
return _divide(i, 1000**illions_number, illions_name)
def _divide(dividend, divisor, magnitude):
return _join(
_say_number_pos(dividend // divisor),
magnitude,
_say_number_pos(dividend % divisor),
)
def _join(*args):
return ' '.join(filter(bool, args))