|
| 1 | +import math |
| 2 | + |
| 3 | + |
| 4 | +def convert(number: int) -> str: |
| 5 | + """ |
| 6 | + Given a number return the number in words. |
| 7 | +
|
| 8 | + >>> convert(123) |
| 9 | + 'OneHundred,TwentyThree' |
| 10 | + """ |
| 11 | + if number == 0: |
| 12 | + words = "Zero" |
| 13 | + return words |
| 14 | + else: |
| 15 | + digits = math.log10(number) |
| 16 | + digits = digits + 1 |
| 17 | + singles = {} |
| 18 | + singles[0] = "" |
| 19 | + singles[1] = "One" |
| 20 | + singles[2] = "Two" |
| 21 | + singles[3] = "Three" |
| 22 | + singles[4] = "Four" |
| 23 | + singles[5] = "Five" |
| 24 | + singles[6] = "Six" |
| 25 | + singles[7] = "Seven" |
| 26 | + singles[8] = "Eight" |
| 27 | + singles[9] = "Nine" |
| 28 | + |
| 29 | + doubles = {} |
| 30 | + doubles[0] = "" |
| 31 | + doubles[2] = "Twenty" |
| 32 | + doubles[3] = "Thirty" |
| 33 | + doubles[4] = "Forty" |
| 34 | + doubles[5] = "Fifty" |
| 35 | + doubles[6] = "Sixty" |
| 36 | + doubles[7] = "Seventy" |
| 37 | + doubles[8] = "Eighty" |
| 38 | + doubles[9] = "Ninety" |
| 39 | + |
| 40 | + teens = {} |
| 41 | + teens[0] = "Ten" |
| 42 | + teens[1] = "Eleven" |
| 43 | + teens[2] = "Twelve" |
| 44 | + teens[3] = "Thirteen" |
| 45 | + teens[4] = "Fourteen" |
| 46 | + teens[5] = "Fifteen" |
| 47 | + teens[6] = "Sixteen" |
| 48 | + teens[7] = "Seventeen" |
| 49 | + teens[8] = "Eighteen" |
| 50 | + teens[9] = "Nineteen" |
| 51 | + |
| 52 | + placevalue = {} |
| 53 | + placevalue[2] = "Hundred," |
| 54 | + placevalue[3] = "Thousand," |
| 55 | + placevalue[5] = "Lakh," |
| 56 | + placevalue[7] = "Crore," |
| 57 | + |
| 58 | + temp_num = number |
| 59 | + words = "" |
| 60 | + counter = 0 |
| 61 | + digits = int(digits) |
| 62 | + while counter < digits: |
| 63 | + current = temp_num % 10 |
| 64 | + if counter % 2 == 0: |
| 65 | + addition = "" |
| 66 | + if counter in placevalue.keys() and current != 0: |
| 67 | + addition = placevalue[counter] |
| 68 | + if counter == 2: |
| 69 | + words = singles[current] + addition + words |
| 70 | + elif counter == 0: |
| 71 | + if ((temp_num % 100) // 10) == 1: |
| 72 | + words = teens[current] + addition + words |
| 73 | + temp_num = temp_num // 10 |
| 74 | + counter += 1 |
| 75 | + else: |
| 76 | + words = singles[current] + addition + words |
| 77 | + |
| 78 | + else: |
| 79 | + words = doubles[current] + addition + words |
| 80 | + |
| 81 | + else: |
| 82 | + if counter == 1: |
| 83 | + if current == 1: |
| 84 | + words = teens[number % 10] + words |
| 85 | + else: |
| 86 | + addition = "" |
| 87 | + if counter in placevalue.keys(): |
| 88 | + addition = placevalue[counter] |
| 89 | + words = doubles[current] + addition + words |
| 90 | + else: |
| 91 | + addition = "" |
| 92 | + if counter in placevalue.keys(): |
| 93 | + if current == 0 and ((temp_num % 100) // 10) == 0: |
| 94 | + addition = "" |
| 95 | + else: |
| 96 | + addition = placevalue[counter] |
| 97 | + if ((temp_num % 100) // 10) == 1: |
| 98 | + words = teens[current] + addition + words |
| 99 | + temp_num = temp_num // 10 |
| 100 | + counter += 1 |
| 101 | + else: |
| 102 | + words = singles[current] + addition + words |
| 103 | + counter += 1 |
| 104 | + temp_num = temp_num // 10 |
| 105 | + return words |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + import doctest |
| 110 | + |
| 111 | + doctest.testmod() |
0 commit comments