-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP17.py
78 lines (57 loc) · 2.1 KB
/
P17.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 30 02:16:38 2017
@author: 84338
"""
global suffixes;
suffixes = ["", "thousand", "million", "billion"];
global ones;
ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
global after_ten;
after_ten = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
global tens;
tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"];
#handles the nomenclature of a triplet
#number: the number in the string form, index: position of the triplet
def gimmeThemWords(number, index):
length = len(number);
if(length > 3):
return False;
#pads the number's string representation with 0s on the left
number = number.zfill(3);
string = "";
hundreds_digit = ord(number[0]) - 48;
tens_digit = ord(number[1]) - 48;
ones_digit = ord(number[2]) - 48;
string += "" if number[0] == '0' else ones[hundreds_digit];
string += " hundred " if not string == "" else "";
if(tens_digit > 1):
string += 'and ' if hundreds_digit!=0 else ''
string += tens[tens_digit - 2];
string += " ";
string += ones[ones_digit];
elif(tens_digit == 1):
string+='and ' if hundreds_digit!=0 else ''
string += after_ten[(int(tens_digit + ones_digit) % 10) - 1];
elif(tens_digit == 0):
if hundreds_digit!=0 and ones_digit!=0:
string+='and '
string += ones[ones_digit];
#counter check to determine the positional system
if(string.endswith("zero")):
string = string[:-len("zero")];
else:
string += " ";
if(not len(string) == 0):
string += suffixes[index];
return string;
def count_capital(string):
return sum(map(len, (string.strip(' ')).split(' ')))
if __name__=='__main__':
count=0
test={}
for i in range(1,1000):
word=gimmeThemWords(str(i),0)
test[word]=count_capital(word)
count+=test[word]
print(count+11)