-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblem17.py
More file actions
44 lines (42 loc) · 1.22 KB
/
problem17.py
File metadata and controls
44 lines (42 loc) · 1.22 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
p= {'1-9': ['one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine'],
'10-19': ['ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen'],
'20-90': ['twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety'],
'x00': 'hundred'}
def hundred(num):
""" num is from 0-9 """
pfx = 0 # prefix applied to each number processed
l = 0 # overall length
if num > 0:
pfx = len(p['1-9'][num - 1]) + len(p['x00'])
l += pfx # simulate counting the first, i.e. one hundred
pfx += 3 # add 3 to prefix, for xxx hundred `AND`
l += pfx * 99 # apply prefix to all the numbers
l += sum([len(x) for x in p['1-9']]) # the good
l += sum([len(x) for x in p['10-19']]) # the bad
l += sum([len(x) + sum([len(y) + len(x) for y in p['1-9']]) for x in p['20-90']]) # the ugly
return l
print sum([hundred(x) for x in range(0, 10)]) + 11 # 11 is length of 'onethousand'