I learned that 18 is spelled as eighteen
This commit is contained in:
77
17/main.py
Normal file
77
17/main.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import os
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
# Find the total number of letters needed to spell out all the numbers up to
|
||||
# and including 1000
|
||||
|
||||
lengths = {
|
||||
1 : 3, # one
|
||||
2 : 3, # two
|
||||
3 : 5, # three
|
||||
4 : 4, # four
|
||||
5 : 4, # five
|
||||
6 : 3, # six
|
||||
7 : 5, # seven
|
||||
8 : 5, # eight
|
||||
9 : 4, # nine
|
||||
10 : 3, # ten
|
||||
11 : 6, # eleven
|
||||
12 : 6, # twelve
|
||||
13 : 8, # thirteen
|
||||
14 : 8, # fourteen
|
||||
15 : 7, # fifteen
|
||||
16 : 7, # sixteen
|
||||
17 : 9, # seventeen
|
||||
18 : 8, # eighteen
|
||||
19 : 8, # nineteen
|
||||
20 : 6, # twenty
|
||||
30 : 6, # thirty
|
||||
40 : 5, # fourty
|
||||
50 : 5, # fifty
|
||||
60 : 5, # sixty
|
||||
70 : 7, # seventy
|
||||
80 : 6, # eighty
|
||||
90 : 6, # ninety
|
||||
100 : 7 # hundred (the number of hundreds will be put in front)
|
||||
}
|
||||
|
||||
def getNumberLength(n):
|
||||
res = 0
|
||||
|
||||
if n == 1000:
|
||||
return 11
|
||||
|
||||
if n % 100 < 20 and n % 100 != 0:
|
||||
res = res + lengths[n % 100]
|
||||
n = n // 100
|
||||
else:
|
||||
if n % 10 != 0:
|
||||
res = res + lengths[n % 10]
|
||||
n = n // 10
|
||||
if n % 10 != 0:
|
||||
res = res + lengths[(n % 10) * 10]
|
||||
n = n // 10
|
||||
|
||||
if n == 0:
|
||||
return res
|
||||
else:
|
||||
a = 3
|
||||
if res == 0:
|
||||
a = 0
|
||||
res = res + a + lengths[100] + lengths[n]
|
||||
|
||||
return res
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
print("Hello, this is Patrick")
|
||||
|
||||
print(getNumberLength(100))
|
||||
|
||||
result = sum(list(map(lambda x: getNumberLength(x), range(1, 1001))))
|
||||
print(result)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user