-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler17.py
56 lines (50 loc) · 1.52 KB
/
euler17.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
d = ['x', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
t = ['x', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
def numberToEnglish(n):
# Numbers up to 999
if n < 20 and n > -1:
return d[n]
elif n < 100:
if n % 10 == 0:
return t[(n//10)]
a = (n % 10)
h =(n // 10)
s = str(t[h]) + '-' + str(d[a])
return s
else:
a = (n % 10)
n = (n // 10)
b = n%10
c = (n // 10)
x = b*10 + a
if x > 10 and x < 20:
s = str(d[c]) + ' hundred and ' + str(d[x])
return s
if a == 0 and b == 0:
return str(d[c]) + ' hundred'
elif a == 0:
s = str(d[c]) + ' hundred and ' + str(t[b])
return s
elif b == 0:
s = str(d[c]) + ' hundred and ' + str(d[a])
return s
else:
s = str(d[c]) + ' hundred and ' + str(t[b]) + '-' + str(d[a])
return s
def count(s):
res = 0
for i in s:
if i not in [' ', '-']:
res += 1
return res
def f():
res = 0
for i in range(1, 1000):
res += count(numberToEnglish(i))
res += len('onethousand')
return res
print(f())
def g():
res = 0
for i in range(1, 1000):
print(numberToEnglish(i))