Problem 17
This commit is contained in:
parent
c96de2bc6b
commit
47aa51e093
97
0017.py
Normal file
97
0017.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
target = 21124
|
||||||
|
|
||||||
|
# 1 = 'one'
|
||||||
|
# 2 = 'two'
|
||||||
|
# 3 = 'three'
|
||||||
|
# 4 = 'four'
|
||||||
|
# 5 = 'five'
|
||||||
|
# 6 = 'sixe'
|
||||||
|
# 7 = 'seven'
|
||||||
|
# 8 = 'eight'
|
||||||
|
# 9 = 'nine'
|
||||||
|
# 10 = 'ten'
|
||||||
|
|
||||||
|
nbrs = [
|
||||||
|
'',
|
||||||
|
'one',
|
||||||
|
'two',
|
||||||
|
'three',
|
||||||
|
'four',
|
||||||
|
'five',
|
||||||
|
'six',
|
||||||
|
'seven',
|
||||||
|
'eight',
|
||||||
|
'nine',
|
||||||
|
'ten',
|
||||||
|
'eleven',
|
||||||
|
'twelve',
|
||||||
|
'thirteen',
|
||||||
|
'fourteen',
|
||||||
|
'fifteen',
|
||||||
|
'sixteen',
|
||||||
|
'seventeen',
|
||||||
|
'eighteen',
|
||||||
|
'nineteen',
|
||||||
|
]
|
||||||
|
|
||||||
|
decades = [
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
'twenty',
|
||||||
|
'thirty',
|
||||||
|
'forty',
|
||||||
|
'fifty',
|
||||||
|
'sixty',
|
||||||
|
'seventy',
|
||||||
|
'eighty',
|
||||||
|
'ninety',
|
||||||
|
]
|
||||||
|
|
||||||
|
loads = [
|
||||||
|
'hundred',
|
||||||
|
'thousand',
|
||||||
|
]
|
||||||
|
|
||||||
|
def number_to_string(nbr):
|
||||||
|
thousand = nbr // 1000
|
||||||
|
hundred = (nbr % 1000) // 100
|
||||||
|
under_hundred = nbr % 100
|
||||||
|
|
||||||
|
string = ''
|
||||||
|
|
||||||
|
if (thousand > 0):
|
||||||
|
string += nbrs[thousand] + 'thousand'
|
||||||
|
|
||||||
|
if (hundred > 0):
|
||||||
|
string += nbrs[hundred] + 'hundred'
|
||||||
|
|
||||||
|
end_string = ''
|
||||||
|
|
||||||
|
if (under_hundred < 20):
|
||||||
|
end_string += nbrs[under_hundred]
|
||||||
|
else:
|
||||||
|
decade = under_hundred // 10
|
||||||
|
single = nbr % 10
|
||||||
|
|
||||||
|
end_string += decades[decade] + nbrs[single]
|
||||||
|
|
||||||
|
if (end_string):
|
||||||
|
if (string):
|
||||||
|
string += 'and'
|
||||||
|
|
||||||
|
string += end_string
|
||||||
|
|
||||||
|
|
||||||
|
return string
|
||||||
|
|
||||||
|
def count_len(result, nbr):
|
||||||
|
string = number_to_string(nbr)
|
||||||
|
|
||||||
|
return result + len(string)
|
||||||
|
|
||||||
|
result = reduce(count_len, list(range(1, 1001)), 0)
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
print(result == target)
|
||||||
Loading…
Reference in New Issue
Block a user