forked from codetrotters/codingchallenge2015
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrotters.py
More file actions
99 lines (91 loc) · 4.73 KB
/
trotters.py
File metadata and controls
99 lines (91 loc) · 4.73 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# int to word. codetrotters
# Alejandro Salvador Vega Nogales, vega360@gmail.com, git: asvnpr
def intToWord(n):
if (n == 0):
return "zero"
else:
# commence string building and component declaration
ones = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine" }
teens = {11: "eleven", 12: "twelve", 13: "thirteen", 14:"fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen"}
tens = {1: "ten", 2: "twenty", 3: "thirty", 4: "forty", 5: "fifty", 6: "sixty", 7: "seventy", 8: "eighty", 9: "ninety" }
others = ["hundred", "thousand", "million"]
digits = []
build = []
word = ""
numStr = str(n) #convert int num to String
if (n < 0):
numStr = numStr[1:]
l = len(numStr)
#capture all digits in int n in a list
for i in range(0, len(numStr)):
digits.append(int(numStr[i]))
# go through list and convert digits to words and build the string
teen = False #used for preventing
while (l > 0):
#10^8 to 10^6
if (l == 9):
if (digits[-(l - 1)] != 0 or digits[-(l - 2)] != 0):
print l-1
build.append(ones[digits[-l]]), build.append(others[0]), build.append("and")
else:
build.append(ones[digits[-l]]), build.append(others[0]), build.append(others[2])
elif (l == 8 and digits[-8] == 1 and digits[-7] > 0): #10^4 is < 20,000,000 and > 10,000,000
tmp = digits[-7] + 10
build.append(teens[tmp]), build.append(others[2])
teen = True
elif (teen != True and (l == 8 or l == 7)): #10^4 is not < 20,000,000 and > 10,000,000
if (l == 8 and digits[-l] != 0):
if (digits[-(l - 1)] != 0):
build.append(tens[digits[-l]])
else:
build.append(tens[digits[-l]]), build.append(others[2])
elif (l == 7 and digits[-l] != 0):
build.append(ones[digits[-l]]), build.append(others[2])
#10^5 to 10^3
elif (l == 6 and digits[-l] != 0):
if (digits[-(l - 1)] != 0 or digits[-(l - 2)] != 0):
build.append(ones[digits[-l]]), build.append(others[0]), build.append("and")
else:
build.append(ones[digits[-l]]), build.append(others[0]), build.append(others[1])
elif (l == 5 and digits[-5] == 1 and digits[-4] > 0): #10^4 is < 20,000 and > 10,000
tmp = digits[-4] + 10
build.append(teens[tmp]), build.append(others[1])
teen = True
elif (teen != True and (l == 5 or l == 4)): #10^4 is not < 20,000 and > 10,000
if (l == 5 and digits[-l] != 0):
if (digits[-(l - 1)] != 0):
build.append(tens[digits[-l]])
else:
build.append(tens[digits[-l]]), build.append(others[1])
elif (l == 4 and digits[-l] != 0):
build.append(ones[digits[-l]]), build.append(others[1])
#10^2 to 10^0
elif (l == 3 and digits[-l] != 0):
if (digits[-(l - 1)] != 0 or digits[-(l - 2)] != 0):
build.append(ones[digits[-l]]), build.append(others[0]), build.append("and")
else:
build.append(ones[digits[-l]]), build.append(others[0])
elif (l == 2 and digits[-2] == 1 and digits[-1] > 0): #cases where 10^1 is < 20 and > 10
tmp = digits[-1] + 10
build.append(teens[tmp])
teen = True
elif (teen != True and (l == 2 or l == 1)): #10^1 is not < 20 and > 10
if (l == 2 and digits[-l] != 0):
if (digits[-(l - 1)] != 0):
build.append(tens[digits[-l]])
else:
build.append(tens[digits[-l]])
elif (l == 1 and digits[-l] != 0):
build.append(ones[digits[-l]])
l -= 1
teen = False #reset teen bool in cases where n is of form #15,#15,#15 or similar
if (n < 0):
build.insert(0, 'negative')
word = ' '.join(build) #add spaces between words and save the completed string
return word
n = input("Enter a whole number between 0 and 999,999,999: ")
while (n > 999999999 or n < -999999999):
print "Error! You entered a value that was too large or too small."
n = int(raw_input("Please enter a whole number between 0 and 999,999,999: "))
print intToWord(n)