diff options
author | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:20:00 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:20:00 -0500 |
commit | 60103d69713f2ed97cf80d377890774fc2fea50b (patch) | |
tree | a1c17aa5b2b6fdef391ba3e20bd13af4ee71efcb /17-Number-Letter-Counts/wordnums.py | |
parent | 93ea7fe5957b62f18e8fbd17a21696bd7de6332d (diff) | |
download | Project_Euler_Solutions-60103d69713f2ed97cf80d377890774fc2fea50b.tar.gz |
Organized everything, update README
Diffstat (limited to '17-Number-Letter-Counts/wordnums.py')
-rw-r--r-- | 17-Number-Letter-Counts/wordnums.py | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/17-Number-Letter-Counts/wordnums.py b/17-Number-Letter-Counts/wordnums.py index f1276b9..34b5705 100644 --- a/17-Number-Letter-Counts/wordnums.py +++ b/17-Number-Letter-Counts/wordnums.py @@ -1,46 +1,48 @@ -import PIL, math +import PIL +import math -ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] +ones = ["", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine"] weirdos = ["thirteen", "twelve", "eleven", "ten"] -tens = ["", "teen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] +tens = ["", "teen", "twenty", "thirty", "forty", + "fifty", "sixty", "seventy", "eighty", "ninety"] nd = "AND" #num must be less than 999# + def numToWord(num): - digits = str(num) - if (len(digits) == 1):#one digit - return ones[num] - if (len(digits) == 2):#two digits - if(num < 14):#special case for ten eleven twelve - return weirdos[13 - num] - if (num == 15): - return "fifteen" - if (num == 18): - return "eighteen" - t = digits[0] - o = digits[1] - if (int(t)==1): - return numToWord(int(o)) + tens[int(t)] - else: - return tens[int(t)] + numToWord(int(o)) - #number is three digits# - h = digits[0] - t = digits[1] - o = digits[2] - if(num % 100 == 0): - return numToWord(int(h)) + "hundred" - else: - return numToWord(int(h)) + "hundred"+nd+numToWord(int(t+o)) - - + digits = str(num) + if (len(digits) == 1): # one digit + return ones[num] + if (len(digits) == 2): # two digits + if(num < 14): # special case for ten eleven twelve + return weirdos[13 - num] + if (num == 15): + return "fifteen" + if (num == 18): + return "eighteen" + t = digits[0] + o = digits[1] + if (int(t) == 1): + return numToWord(int(o)) + tens[int(t)] + else: + return tens[int(t)] + numToWord(int(o)) + #number is three digits# + h = digits[0] + t = digits[1] + o = digits[2] + if(num % 100 == 0): + return numToWord(int(h)) + "hundred" + else: + return numToWord(int(h)) + "hundred"+nd+numToWord(int(t+o)) + n = int(input("Number: ")) -out ="" +out = "" for i in range(1, n): - out += numToWord(int(i)) + out += numToWord(int(i)) -out+="onethousand" +out += "onethousand" print(out) print(str(len(out)) + " letters") - |