From 39a0bb94cc01d4b278260ac1ceeb827b89e23abd Mon Sep 17 00:00:00 2001 From: Shreyas Kamath <42207943+s18k@users.noreply.github.com> Date: Fri, 7 Oct 2022 14:08:41 +0530 Subject: [PATCH 1/2] Create convert_number_to_words.py A Python Program to convert numerical digits to English words. An Application of this can be in a Payment Application for confirmation --- web_programming/convert_number_to_words.py | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 web_programming/convert_number_to_words.py diff --git a/web_programming/convert_number_to_words.py b/web_programming/convert_number_to_words.py new file mode 100644 index 000000000000..e66f68424d9b --- /dev/null +++ b/web_programming/convert_number_to_words.py @@ -0,0 +1,110 @@ +import math +def convert(number: int) -> str: + """ + Given a number return the number in words. + + >>> convert(123) + 'OneHundred,TwentyThree' + """ + if(number==0): + words = "Zero" + return words + else: + digits = math.log10(number) + digits = digits+1 + singles = {} + singles[0]="" + singles[1]="One" + singles[2]="Two" + singles[3]="Three" + singles[4]="Four" + singles[5]="Five" + singles[6]="Six" + singles[7]="Seven" + singles[8]="Eight" + singles[9]="Nine" + + doubles = {} + doubles[0] = "" + doubles[2] = "Twenty" + doubles[3] = "Thirty" + doubles[4] = "Forty" + doubles[5] = "Fifty" + doubles[6] = "Sixty" + doubles[7] = "Seventy" + doubles[8] = "Eighty" + doubles[9] = "Ninety" + + teens = {} + teens[0] = "Ten" + teens[1] = "Eleven" + teens[2] = "Twelve" + teens[3] = "Thirteen" + teens[4] = "Fourteen" + teens[5] = "Fifteen" + teens[6] = "Sixteen" + teens[7] = "Seventeen" + teens[8] = "Eighteen" + teens[9] = "Nineteen" + + placevalue = {} + placevalue[2] = "Hundred," + placevalue[3] = "Thousand," + placevalue[5] = "Lakh," + placevalue[7] = "Crore," + + temp_num = number + words = "" + counter = 0 + digits = int(digits) + while(counter Date: Fri, 7 Oct 2022 08:39:46 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/convert_number_to_words.py | 205 +++++++++++---------- 1 file changed, 103 insertions(+), 102 deletions(-) diff --git a/web_programming/convert_number_to_words.py b/web_programming/convert_number_to_words.py index e66f68424d9b..50612dec20dd 100644 --- a/web_programming/convert_number_to_words.py +++ b/web_programming/convert_number_to_words.py @@ -1,110 +1,111 @@ import math + + def convert(number: int) -> str: - """ - Given a number return the number in words. + """ + Given a number return the number in words. + + >>> convert(123) + 'OneHundred,TwentyThree' + """ + if number == 0: + words = "Zero" + return words + else: + digits = math.log10(number) + digits = digits + 1 + singles = {} + singles[0] = "" + singles[1] = "One" + singles[2] = "Two" + singles[3] = "Three" + singles[4] = "Four" + singles[5] = "Five" + singles[6] = "Six" + singles[7] = "Seven" + singles[8] = "Eight" + singles[9] = "Nine" + + doubles = {} + doubles[0] = "" + doubles[2] = "Twenty" + doubles[3] = "Thirty" + doubles[4] = "Forty" + doubles[5] = "Fifty" + doubles[6] = "Sixty" + doubles[7] = "Seventy" + doubles[8] = "Eighty" + doubles[9] = "Ninety" + + teens = {} + teens[0] = "Ten" + teens[1] = "Eleven" + teens[2] = "Twelve" + teens[3] = "Thirteen" + teens[4] = "Fourteen" + teens[5] = "Fifteen" + teens[6] = "Sixteen" + teens[7] = "Seventeen" + teens[8] = "Eighteen" + teens[9] = "Nineteen" + + placevalue = {} + placevalue[2] = "Hundred," + placevalue[3] = "Thousand," + placevalue[5] = "Lakh," + placevalue[7] = "Crore," - >>> convert(123) - 'OneHundred,TwentyThree' - """ - if(number==0): - words = "Zero" - return words - else: - digits = math.log10(number) - digits = digits+1 - singles = {} - singles[0]="" - singles[1]="One" - singles[2]="Two" - singles[3]="Three" - singles[4]="Four" - singles[5]="Five" - singles[6]="Six" - singles[7]="Seven" - singles[8]="Eight" - singles[9]="Nine" + temp_num = number + words = "" + counter = 0 + digits = int(digits) + while counter < digits: + current = temp_num % 10 + if counter % 2 == 0: + addition = "" + if counter in placevalue.keys() and current != 0: + addition = placevalue[counter] + if counter == 2: + words = singles[current] + addition + words + elif counter == 0: + if ((temp_num % 100) // 10) == 1: + words = teens[current] + addition + words + temp_num = temp_num // 10 + counter += 1 + else: + words = singles[current] + addition + words - doubles = {} - doubles[0] = "" - doubles[2] = "Twenty" - doubles[3] = "Thirty" - doubles[4] = "Forty" - doubles[5] = "Fifty" - doubles[6] = "Sixty" - doubles[7] = "Seventy" - doubles[8] = "Eighty" - doubles[9] = "Ninety" + else: + words = doubles[current] + addition + words - teens = {} - teens[0] = "Ten" - teens[1] = "Eleven" - teens[2] = "Twelve" - teens[3] = "Thirteen" - teens[4] = "Fourteen" - teens[5] = "Fifteen" - teens[6] = "Sixteen" - teens[7] = "Seventeen" - teens[8] = "Eighteen" - teens[9] = "Nineteen" + else: + if counter == 1: + if current == 1: + words = teens[number % 10] + words + else: + addition = "" + if counter in placevalue.keys(): + addition = placevalue[counter] + words = doubles[current] + addition + words + else: + addition = "" + if counter in placevalue.keys(): + if current == 0 and ((temp_num % 100) // 10) == 0: + addition = "" + else: + addition = placevalue[counter] + if ((temp_num % 100) // 10) == 1: + words = teens[current] + addition + words + temp_num = temp_num // 10 + counter += 1 + else: + words = singles[current] + addition + words + counter += 1 + temp_num = temp_num // 10 + return words - placevalue = {} - placevalue[2] = "Hundred," - placevalue[3] = "Thousand," - placevalue[5] = "Lakh," - placevalue[7] = "Crore," - temp_num = number - words = "" - counter = 0 - digits = int(digits) - while(counter