From 8da990843fff52d5a36516538547f667d6b9bde5 Mon Sep 17 00:00:00 2001 From: anebz Date: Wed, 10 Apr 2019 19:07:01 +0200 Subject: [PATCH] 16.8. int to english moderate problems chapter --- 16. Moderate problems/16.8. english_int.md | 41 ++++++++++++ 16. Moderate problems/16.8. english_int.py | 78 ++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 16. Moderate problems/16.8. english_int.md create mode 100644 16. Moderate problems/16.8. english_int.py diff --git a/16. Moderate problems/16.8. english_int.md b/16. Moderate problems/16.8. english_int.md new file mode 100644 index 0000000..465461c --- /dev/null +++ b/16. Moderate problems/16.8. english_int.md @@ -0,0 +1,41 @@ +# 16.8. English int + +## Given any integer, print an english phrase that describes the integer + +> example: + +* input = 1234 +* output = "one thousand two hundred thirty four" + +## First idea + +Two tasks here, 1. int to char, and 2. int to thousand/hundred thirt(y) whatever. what about 11, 12, 35? + +```python +inttochar = { + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine" +} + +inttodec = { + 10: "ten", + 11: "eleven", + 12: "twelve", + 3: "thir", + 5: "fif" +} + +inttoext = { + 1: "teen", + 0: "ty" +} +``` + +This isn't a difficult problem, it's just cumbersome and long to make good-looking code. \ No newline at end of file diff --git a/16. Moderate problems/16.8. english_int.py b/16. Moderate problems/16.8. english_int.py new file mode 100644 index 0000000..f806f93 --- /dev/null +++ b/16. Moderate problems/16.8. english_int.py @@ -0,0 +1,78 @@ +import unittest + +inttochar = { + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine" +} + +inttodec = { + 10: "ten", + 11: "eleven", + 12: "twelve", + 3: "thir", + 4: "for", + 5: "fif" +} + +inttoext = { + 1: "teen", + 0: "ty" +} + +def inttostrings(num, numlst): + + ret = "" + if numlst[0] == 1: + if num in inttodec: + return inttodec[num] + if numlst[1] in inttodec: + return inttodec[numlst[1]] + inttoext[1] + else: + return inttochar[numlst[1]] + inttoext[1] + if numlst[0] in inttodec: + if numlst[0] != 0: + ret += inttodec[numlst[0]] + inttoext[0] + " " + return ret + inttochar[numlst[1]] + if numlst[0] != 0: + ret += inttochar[numlst[0]] + inttoext[0] + " " + return ret + inttochar[numlst[1]] + +def int_to_eng(num): + + if num < 0: + return -1 + if num == 0: + return "zero" + if num < 10: + return inttochar[num] + + res = "" + numlst = list(map(int, str(num))) + + for i in range(len(numlst)-1): + numlst[-2-i] = inttostrings(num, numlst[-2-i:]) + numlst.pop(-1) + + return "".join(numlst) + + +class Test(unittest.TestCase): + data = [(12, "twelve"), + (16, "sixteen"), + (45, "forty five"), + (33, "thirty three")] + + def test_rotate_matrix(self): + for test_num, expected in self.data: + actual = int_to_eng(test_num) + self.assertEqual(actual, expected) + +if __name__ == "__main__": + unittest.main()