From 714aa264863a2e134ff5434f5fbded58316f3c76 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 7 Aug 2024 16:18:05 +0300 Subject: [PATCH 1/3] feat: add ts solution to lc problem: No.0273 --- .../0273.Integer to English Words/README.md | 26 +++++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++++ .../0273.Integer to English Words/Solution.ts | 21 +++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 solution/0200-0299/0273.Integer to English Words/Solution.ts diff --git a/solution/0200-0299/0273.Integer to English Words/README.md b/solution/0200-0299/0273.Integer to English Words/README.md index 54a28c8948824..1d3ceb82faac6 100644 --- a/solution/0200-0299/0273.Integer to English Words/README.md +++ b/solution/0200-0299/0273.Integer to English Words/README.md @@ -296,6 +296,32 @@ public class Solution { } ``` +#### TypeScript + +```ts +function numberToWords(num: number): string { + if (num === 0) return 'Zero' + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num) +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/README_EN.md b/solution/0200-0299/0273.Integer to English Words/README_EN.md index 51615ba7e4a7a..5ddf4079d297c 100644 --- a/solution/0200-0299/0273.Integer to English Words/README_EN.md +++ b/solution/0200-0299/0273.Integer to English Words/README_EN.md @@ -294,6 +294,32 @@ public class Solution { } ``` +#### TypeScript + +```ts +function numberToWords(num: number): string { + if (num === 0) return 'Zero' + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num) +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/Solution.ts b/solution/0200-0299/0273.Integer to English Words/Solution.ts new file mode 100644 index 0000000000000..32c8e3b3a24a3 --- /dev/null +++ b/solution/0200-0299/0273.Integer to English Words/Solution.ts @@ -0,0 +1,21 @@ +function numberToWords(num: number): string { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} From 53ef6266ca7351b17fdbeaedf2774d8731758c47 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 7 Aug 2024 16:19:44 +0300 Subject: [PATCH 2/3] feat: add js solution to lc problem: No.0273 --- .../0273.Integer to English Words/README.md | 26 +++++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++++ .../0273.Integer to English Words/Solution.js | 21 +++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 solution/0200-0299/0273.Integer to English Words/Solution.js diff --git a/solution/0200-0299/0273.Integer to English Words/README.md b/solution/0200-0299/0273.Integer to English Words/README.md index 1d3ceb82faac6..8896618ca4e21 100644 --- a/solution/0200-0299/0273.Integer to English Words/README.md +++ b/solution/0200-0299/0273.Integer to English Words/README.md @@ -322,6 +322,32 @@ function numberToWords(num: number): string { } ``` +#### JavaScript + +```js +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/README_EN.md b/solution/0200-0299/0273.Integer to English Words/README_EN.md index 5ddf4079d297c..cf8e8c4261228 100644 --- a/solution/0200-0299/0273.Integer to English Words/README_EN.md +++ b/solution/0200-0299/0273.Integer to English Words/README_EN.md @@ -320,6 +320,32 @@ function numberToWords(num: number): string { } ``` +#### JavaScript + +```js +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/Solution.js b/solution/0200-0299/0273.Integer to English Words/Solution.js new file mode 100644 index 0000000000000..4abc586bf2dee --- /dev/null +++ b/solution/0200-0299/0273.Integer to English Words/Solution.js @@ -0,0 +1,21 @@ +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} From 6f18b4269da288721141a01c48870bc1cc9b8bea Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 7 Aug 2024 13:21:06 +0000 Subject: [PATCH 3/3] style: format code and docs with prettier --- .../0200-0299/0273.Integer to English Words/README.md | 8 ++++---- .../0200-0299/0273.Integer to English Words/README_EN.md | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/solution/0200-0299/0273.Integer to English Words/README.md b/solution/0200-0299/0273.Integer to English Words/README.md index 8896618ca4e21..a7fce1803bf4e 100644 --- a/solution/0200-0299/0273.Integer to English Words/README.md +++ b/solution/0200-0299/0273.Integer to English Words/README.md @@ -300,10 +300,10 @@ public class Solution { ```ts function numberToWords(num: number): string { - if (num === 0) return 'Zero' + if (num === 0) return 'Zero'; - // prettier-ignore - const f = (x: number): string => { + // prettier-ignore + const f = (x: number): string => { const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] let ans = '' @@ -318,7 +318,7 @@ function numberToWords(num: number): string { return ans.trim() } - return f(num) + return f(num); } ``` diff --git a/solution/0200-0299/0273.Integer to English Words/README_EN.md b/solution/0200-0299/0273.Integer to English Words/README_EN.md index cf8e8c4261228..3260ff5261c97 100644 --- a/solution/0200-0299/0273.Integer to English Words/README_EN.md +++ b/solution/0200-0299/0273.Integer to English Words/README_EN.md @@ -298,10 +298,10 @@ public class Solution { ```ts function numberToWords(num: number): string { - if (num === 0) return 'Zero' + if (num === 0) return 'Zero'; - // prettier-ignore - const f = (x: number): string => { + // prettier-ignore + const f = (x: number): string => { const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] let ans = '' @@ -316,7 +316,7 @@ function numberToWords(num: number): string { return ans.trim() } - return f(num) + return f(num); } ```