aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--integer_english_words.c69
-rw-r--r--integer_english_words.py39
2 files changed, 108 insertions, 0 deletions
diff --git a/integer_english_words.c b/integer_english_words.c
new file mode 100644
index 0000000..e369f19
--- /dev/null
+++ b/integer_english_words.c
@@ -0,0 +1,69 @@
+// 273. Integer to English Words
+#include "leetcode.h"
+
+/*
+ * convert a non-negative integer 'num' to its english words represetation.
+ */
+
+const char *below_twenty[] = {"", "One", "Two", "Three",
+ "Four", "Five", "Six", "Seven",
+ "Eight", "Nine", "Ten", "Eleven",
+ "Twelve", "Thirteen", "Fourteen", "Fifteen",
+ "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
+const char *tens[] = {"", "", "Twenty", "Thirty", "Forty",
+ "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
+const char *thousands[] = {"", "Thousand", "Million", "Billion"};
+
+char *helper(int num) {
+ char *res = (char *)calloc(100, sizeof(char));
+ if (!num)
+ return "";
+ else if (num < 20)
+ strcat(res, below_twenty[num]);
+ else if (num < 100) {
+ strcat(res, tens[num / 10]);
+ if (num % 10)
+ strcat(res, " ");
+ strcat(res, helper(num % 10));
+ } else {
+ strcat(res, below_twenty[num / 100]);
+ strcat(res, " Hundred");
+ if (num % 100)
+ strcat(res, " ");
+ strcat(res, helper(num % 100));
+ }
+ return res;
+}
+
+char *numberToWords(int num) {
+ if (!num)
+ return "Zero";
+ char *ans = (char *)calloc(100, sizeof(char));
+ int i = 0;
+ while (num) {
+ if (num % 1000) {
+ char *seg = helper(num % 1000);
+ if (i)
+ strcat(seg, " ");
+ strcat(seg, thousands[i]);
+ if (strlen(ans))
+ strcat(seg, " ");
+ strcat(seg, ans);
+ strcpy(ans, seg);
+ free(seg);
+ }
+ num /= 1000;
+ i++;
+ }
+ return ans;
+}
+
+int main() {
+ printf("%s\n", numberToWords(123)); // expect: One Hundred Twenty Three
+ printf(
+ "%s\n",
+ numberToWords(12345)); // expect: Twelve Thousand Three Hundred Forty Five
+ printf("%s\n",
+ numberToWords(1234567)); // expect: One Million Two Hundred Thirty Four
+ // Thousand Five Hundred Sixty Seven
+}
diff --git a/integer_english_words.py b/integer_english_words.py
new file mode 100644
index 0000000..e8f817a
--- /dev/null
+++ b/integer_english_words.py
@@ -0,0 +1,39 @@
+# 273. Integer to English Words
+
+"""
+convert a non-negative integer 'num' to its english words represetation.
+"""
+
+
+class Solution(object):
+ def numberToWords(self, num):
+ """
+ :type num: int
+ :rtype: str
+ """
+
+ below_twenty = (
+ "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve "
+ "Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen".split()
+ )
+ tens = "Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety".split()
+
+ def words(n):
+ if n < 20:
+ return below_twenty[n - 1 : n]
+ if n < 100:
+ return [tens[n / 10 - 2]] + words(n % 10)
+ if n < 1000:
+ return [below_twenty[n / 100 - 1]] + ["Hundred"] + words(n % 100)
+ for p, w in enumerate(("Thousand", "Million", "Billion"), 1):
+ if n < 1000 ** (p + 1):
+ return words(n / 1000**p) + [w] + words(n % 1000**p)
+
+ return " ".join(words(num)) or "Zero"
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numberToWords(123))
+ print(obj.numberToWords(12345))
+ print(obj.numberToWords(1234567))