From be9be8145c159200b4f7514addba8de0f216e207 Mon Sep 17 00:00:00 2001 From: Alejandro Vega Date: Mon, 15 Feb 2016 21:25:37 -0500 Subject: [PATCH 1/4] Create trotters.py --- trotters.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 trotters.py diff --git a/trotters.py b/trotters.py new file mode 100644 index 0000000..cd8a221 --- /dev/null +++ b/trotters.py @@ -0,0 +1,88 @@ +#!/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 + 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 + while (l > 0): + #10^8 to 10^6 + if (l == 9): + if (digits[-l] != 0 or digits[-(l - 1)] != 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[2]) + elif (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[-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 and teen == False): + tmp = digits[len(digits) - 1] + 10 + build.append(teens[tmp]) + teen = True + elif (teen != True): + 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 + word = ' '.join(build) + return word + +n = int(raw_input("Enter a whole number between 0 and 999,999,999: ")) +while (n > 999999999 or n < 0): + 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) + + + + + + + + + + From 9317d63e448487223c0e1048998829c530ae125e Mon Sep 17 00:00:00 2001 From: Alejandro Vega Date: Mon, 15 Feb 2016 21:26:46 -0500 Subject: [PATCH 2/4] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9efd541..b2f3297 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,6 @@ int_to_word(200017) == "two hundred thousand seventeen" int_to_word(784) == "seven hundred eighty four" int_to_word(1000215) == "one million two hundred fifteen" ``` + +## Very Small Change: +- added 'and' in the output phrase where appropriate. Felt more natural From 3d8d69404d8286a7835e58ef315f44c33dfc73b7 Mon Sep 17 00:00:00 2001 From: Alejandro Vega Date: Tue, 16 Feb 2016 15:24:02 -0500 Subject: [PATCH 3/4] Update trotters.py added cases for 10-20 million, 10-20 thousand. handles combinations of *teens. added some whitespace for readability. added a few comments --- trotters.py | 67 +++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/trotters.py b/trotters.py index cd8a221..ced0a2d 100644 --- a/trotters.py +++ b/trotters.py @@ -21,45 +21,61 @@ def intToWord(n): digits.append(int(numStr[i])) # go through list and convert digits to words and build the string - teen = False + teen = False #used for preventing while (l > 0): #10^8 to 10^6 if (l == 9): - if (digits[-l] != 0 or digits[-(l - 1)] != 0): + 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[-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]) + 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[-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]) + + 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 and teen == False): - tmp = digits[len(digits) - 1] + 10 + + 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): + + 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]]) @@ -68,7 +84,8 @@ def intToWord(n): elif (l == 1 and digits[-l] != 0): build.append(ones[digits[-l]]) l -= 1 - word = ' '.join(build) + teen = False #reset teen bool in cases where n is of form #15,#15,#15 or similar + word = ' '.join(build) #add spaces between words and save the completed string return word n = int(raw_input("Enter a whole number between 0 and 999,999,999: ")) @@ -76,13 +93,3 @@ def intToWord(n): 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) - - - - - - - - - - From 11b0063fc47319f3a614263a01e70b9d2ae9f9ea Mon Sep 17 00:00:00 2001 From: Alejandro Vega Date: Tue, 16 Feb 2016 16:03:25 -0500 Subject: [PATCH 4/4] Update trotters.py I didn't notice negatives were covered. added few lines of code to correct issue --- trotters.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/trotters.py b/trotters.py index ced0a2d..f4ef2dd 100644 --- a/trotters.py +++ b/trotters.py @@ -14,11 +14,13 @@ def intToWord(n): 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])) + digits.append(int(numStr[i])) # go through list and convert digits to words and build the string teen = False #used for preventing @@ -85,11 +87,13 @@ def intToWord(n): 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 = int(raw_input("Enter a whole number between 0 and 999,999,999: ")) -while (n > 999999999 or n < 0): +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)