|
| 1 | +def number_to_words_recursion(num: int) -> str: |
| 2 | + if num == 0: |
| 3 | + return "Zero" |
| 4 | + |
| 5 | + below_ten = [ |
| 6 | + "", |
| 7 | + "One", |
| 8 | + "Two", |
| 9 | + "Three", |
| 10 | + "Four", |
| 11 | + "Five", |
| 12 | + "Six", |
| 13 | + "Seven", |
| 14 | + "Eight", |
| 15 | + "Nine", |
| 16 | + ] |
| 17 | + below_twenty = [ |
| 18 | + "Ten", |
| 19 | + "Eleven", |
| 20 | + "Twelve", |
| 21 | + "Thirteen", |
| 22 | + "Fourteen", |
| 23 | + "Fifteen", |
| 24 | + "Sixteen", |
| 25 | + "Seventeen", |
| 26 | + "Eighteen", |
| 27 | + "Nineteen", |
| 28 | + ] |
| 29 | + below_hundred = [ |
| 30 | + "", |
| 31 | + "Ten", |
| 32 | + "Twenty", |
| 33 | + "Thirty", |
| 34 | + "Forty", |
| 35 | + "Fifty", |
| 36 | + "Sixty", |
| 37 | + "Seventy", |
| 38 | + "Eighty", |
| 39 | + "Ninety", |
| 40 | + ] |
| 41 | + |
| 42 | + # Recursive function to convert numbers to words |
| 43 | + # Handles numbers based on their ranges: <10, <20, <100, <1000, <1000000, <1000000000, and >=1000000000 |
| 44 | + def convert_to_words(number: int) -> str: |
| 45 | + if number < 10: |
| 46 | + return below_ten[number] |
| 47 | + if number < 20: |
| 48 | + return below_twenty[number - 10] |
| 49 | + if number < 100: |
| 50 | + return below_hundred[number // 10] + ( |
| 51 | + " " + convert_to_words(number % 10) if number % 10 != 0 else "" |
| 52 | + ) |
| 53 | + if number < 1000: |
| 54 | + return ( |
| 55 | + convert_to_words(number // 100) |
| 56 | + + " Hundred" |
| 57 | + + (" " + convert_to_words(number % 100) if number % 100 != 0 else "") |
| 58 | + ) |
| 59 | + if number < 1000000: |
| 60 | + return ( |
| 61 | + convert_to_words(number // 1000) |
| 62 | + + " Thousand" |
| 63 | + + (" " + convert_to_words(number % 1000) if number % 1000 != 0 else "") |
| 64 | + ) |
| 65 | + if number < 1000000000: |
| 66 | + return ( |
| 67 | + convert_to_words(number // 1000000) |
| 68 | + + " Million" |
| 69 | + + ( |
| 70 | + " " + convert_to_words(number % 1000000) |
| 71 | + if number % 1000000 != 0 |
| 72 | + else "" |
| 73 | + ) |
| 74 | + ) |
| 75 | + return ( |
| 76 | + convert_to_words(number // 1000000000) |
| 77 | + + " Billion" |
| 78 | + + ( |
| 79 | + " " + convert_to_words(number % 1000000000) |
| 80 | + if number % 1000000000 != 0 |
| 81 | + else "" |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + return convert_to_words(num) |
| 86 | + |
| 87 | + |
| 88 | +def number_to_words_recursion_2(num: int) -> str: |
| 89 | + if num == 0: |
| 90 | + return "Zero" |
| 91 | + |
| 92 | + # Store the word representations of the numbers from 1 to 19 |
| 93 | + below20 = [ |
| 94 | + "One", |
| 95 | + "Two", |
| 96 | + "Three", |
| 97 | + "Four", |
| 98 | + "Five", |
| 99 | + "Six", |
| 100 | + "Seven", |
| 101 | + "Eight", |
| 102 | + "Nine", |
| 103 | + "Ten", |
| 104 | + "Eleven", |
| 105 | + "Twelve", |
| 106 | + "Thirteen", |
| 107 | + "Fourteen", |
| 108 | + "Fifteen", |
| 109 | + "Sixteen", |
| 110 | + "Seventeen", |
| 111 | + "Eighteen", |
| 112 | + "Nineteen", |
| 113 | + ] |
| 114 | + |
| 115 | + # Store the word representations of tens numbers |
| 116 | + tens = [ |
| 117 | + "Twenty", |
| 118 | + "Thirty", |
| 119 | + "Forty", |
| 120 | + "Fifty", |
| 121 | + "Sixty", |
| 122 | + "Seventy", |
| 123 | + "Eighty", |
| 124 | + "Ninety", |
| 125 | + ] |
| 126 | + |
| 127 | + # Store the word representations of thousands numbers |
| 128 | + thousands = ["", "Thousand", "Million", "Billion"] |
| 129 | + |
| 130 | + # Function to find word representation of a three-digit number |
| 131 | + def represent(n): |
| 132 | + if n == 0: |
| 133 | + return "" |
| 134 | + elif n < 20: |
| 135 | + return below20[n - 1] + " " |
| 136 | + elif n < 100: |
| 137 | + return tens[n // 10 - 2] + " " + represent(n % 10) |
| 138 | + else: |
| 139 | + return below20[n // 100 - 1] + " Hundred " + represent(n % 100) |
| 140 | + |
| 141 | + # Initialize the result string |
| 142 | + result = "" |
| 143 | + |
| 144 | + # For each string in thousands |
| 145 | + for t in thousands: |
| 146 | + # If the last three digits are not zero |
| 147 | + if num % 1000 != 0: |
| 148 | + # Get the representation of the three-digit number, append result to it, and |
| 149 | + # store it back in result |
| 150 | + result = represent(num % 1000) + t + " " + result |
| 151 | + |
| 152 | + # Divide num by 1000 and update num with the quotient |
| 153 | + num //= 1000 |
| 154 | + |
| 155 | + # Return the result string |
| 156 | + return result.strip() |
| 157 | + |
| 158 | + |
| 159 | +def number_to_words_iterative(num: int) -> str: |
| 160 | + # Handle the special case where the number is zero |
| 161 | + if num == 0: |
| 162 | + return "Zero" |
| 163 | + |
| 164 | + # Arrays to store words for single digits, tens, and thousands |
| 165 | + ones = [ |
| 166 | + "", |
| 167 | + "One", |
| 168 | + "Two", |
| 169 | + "Three", |
| 170 | + "Four", |
| 171 | + "Five", |
| 172 | + "Six", |
| 173 | + "Seven", |
| 174 | + "Eight", |
| 175 | + "Nine", |
| 176 | + "Ten", |
| 177 | + "Eleven", |
| 178 | + "Twelve", |
| 179 | + "Thirteen", |
| 180 | + "Fourteen", |
| 181 | + "Fifteen", |
| 182 | + "Sixteen", |
| 183 | + "Seventeen", |
| 184 | + "Eighteen", |
| 185 | + "Nineteen", |
| 186 | + ] |
| 187 | + tens = [ |
| 188 | + "", |
| 189 | + "", |
| 190 | + "Twenty", |
| 191 | + "Thirty", |
| 192 | + "Forty", |
| 193 | + "Fifty", |
| 194 | + "Sixty", |
| 195 | + "Seventy", |
| 196 | + "Eighty", |
| 197 | + "Ninety", |
| 198 | + ] |
| 199 | + thousands = ["", "Thousand", "Million", "Billion"] |
| 200 | + |
| 201 | + # StringBuilder to accumulate the result |
| 202 | + result = "" |
| 203 | + group_index = 0 |
| 204 | + |
| 205 | + # Process the number in chunks of 1000 |
| 206 | + while num > 0: |
| 207 | + # Process the last three digits |
| 208 | + if num % 1000 != 0: |
| 209 | + group_result = "" |
| 210 | + part = num % 1000 |
| 211 | + |
| 212 | + # Handle hundreds |
| 213 | + if part >= 100: |
| 214 | + group_result += ones[part // 100] + " Hundred " |
| 215 | + part %= 100 |
| 216 | + |
| 217 | + # Handle tens and units |
| 218 | + if part >= 20: |
| 219 | + group_result += tens[part // 10] + " " |
| 220 | + part %= 10 |
| 221 | + |
| 222 | + # Handle units |
| 223 | + if part > 0: |
| 224 | + group_result += ones[part] + " " |
| 225 | + |
| 226 | + # Append the scale (thousand, million, billion) for the current group |
| 227 | + group_result += thousands[group_index] + " " |
| 228 | + # Insert the group result at the beginning of the final result |
| 229 | + result = group_result + result |
| 230 | + # Move to the next chunk of 1000 |
| 231 | + num //= 1000 |
| 232 | + group_index += 1 |
| 233 | + |
| 234 | + return result.strip() |
| 235 | + |
| 236 | + |
| 237 | +def number_to_words_pair(num: int) -> str: |
| 238 | + if num == 0: |
| 239 | + return "Zero" |
| 240 | + |
| 241 | + # Dictionary to store words for numbers |
| 242 | + number_to_words_map = { |
| 243 | + 1000000000: "Billion", |
| 244 | + 1000000: "Million", |
| 245 | + 1000: "Thousand", |
| 246 | + 100: "Hundred", |
| 247 | + 90: "Ninety", |
| 248 | + 80: "Eighty", |
| 249 | + 70: "Seventy", |
| 250 | + 60: "Sixty", |
| 251 | + 50: "Fifty", |
| 252 | + 40: "Forty", |
| 253 | + 30: "Thirty", |
| 254 | + 20: "Twenty", |
| 255 | + 19: "Nineteen", |
| 256 | + 18: "Eighteen", |
| 257 | + 17: "Seventeen", |
| 258 | + 16: "Sixteen", |
| 259 | + 15: "Fifteen", |
| 260 | + 14: "Fourteen", |
| 261 | + 13: "Thirteen", |
| 262 | + 12: "Twelve", |
| 263 | + 11: "Eleven", |
| 264 | + 10: "Ten", |
| 265 | + 9: "Nine", |
| 266 | + 8: "Eight", |
| 267 | + 7: "Seven", |
| 268 | + 6: "Six", |
| 269 | + 5: "Five", |
| 270 | + 4: "Four", |
| 271 | + 3: "Three", |
| 272 | + 2: "Two", |
| 273 | + 1: "One", |
| 274 | + } |
| 275 | + |
| 276 | + for value, word in number_to_words_map.items(): |
| 277 | + # Check if the number is greater than or equal to the current unit |
| 278 | + if num >= value: |
| 279 | + # Convert the quotient to words if the current unit is 100 or greater |
| 280 | + prefix = (number_to_words_pair(num // value) + " ") if num >= 100 else "" |
| 281 | + |
| 282 | + # Get the word for the current unit |
| 283 | + unit = word |
| 284 | + |
| 285 | + # Convert the remainder to words if it's not zero |
| 286 | + suffix = "" if num % value == 0 else " " + number_to_words_pair(num % value) |
| 287 | + |
| 288 | + return prefix + unit + suffix |
| 289 | + |
| 290 | + return "" |
0 commit comments