-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr17.py
More file actions
68 lines (60 loc) · 1.4 KB
/
pr17.py
File metadata and controls
68 lines (60 loc) · 1.4 KB
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
one_nineteen = {0: 0,
1: 3,
2: 3,
3: 5,
4: 4,
5: 4,
6: 3,
7: 5,
8: 5,
9: 4,
10: 3,
11: 6,
12: 6,
13: 8,
14: 8,
15: 7,
16: 7,
17: 9,
18: 8,
19: 8}
tens = {20: 6,
30: 6,
40: 5,
50: 5,
60: 5,
70: 7,
80: 6,
90: 6}
def single_digit(n):
global one_nineteen
return one_nineteen[n]
def two_digit(n):
global one_nineteen
global tens
if n < 20:
return one_nineteen[n]
return tens[(n / 10) * 10] + single_digit(n % 10)
def three_digit(n):
# 'hundred and' is 10 letters
# 'hundred' is 7 letters
if n % 100 == 0:
return 7 + single_digit(n / 100)
else:
return 10 + single_digit(n / 100) + two_digit(n % 100)
def number_word_length(n):
if n < 10:
return single_digit(n)
if n < 100:
return two_digit(n)
if n < 1000:
return three_digit(n)
if n == 1000:
return 11
def sum_of_digits_up_to(n):
"""Sum of digits from 1 to n, inclusive"""
output = 0
for i in range(1, n + 1):
output += number_word_length(i)
return output
print sum_of_digits_up_to(1000)