Good evening! Here's our prompt for today.
Can you convert a number to its text equivalent? This is otherwise known as number spelling.

For example, given 1234, we expect to get "one thousand two hundred and thirty four" back.
Constraints
- The given number will have <=
100digits - Let n be the number of digits
- Expected time complexity :
O(n) - Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxxdef number_to_words(num): # fill in this method return num​​import unittest​​class Test(unittest.TestCase): def test_1(self): assert number_to_words(1234) == "one thousand two hundred and thirty four" print( "PASSED: assert number_to_words(1234) == 'one thousand two hundred and thirty four'" )​​if __name__ == "__main__": unittest.main(verbosity=2) print("Nice job, 1/1 tests passed!")​We'll now take you through what you need to know.
How do I use this guide?
This problem is deceivingly complicated, so let's take it step and step. Let's use a simple number, like 123 for now.
First, let's convert 123 to a string type to start our operation.
xxxxxxxxxx123 -> "123"Then check if it's zero (because if it is, we can just return "zero").
Next, let's make sure we've defined the building blocks that will make up the final string. They include:
unitslikeone,thirteen, etc.tensvalues liketwenty,thirty, etc.scaleslikethousand, andmillion
Now we account for the various scales. Most of the scaling levels come up every 3 digits. Using this code block, we can get every 3 digits as chunks.

xxxxxxxxxxstart = string.length;chunks = [];while (start > 0) { end = start; chunks.push(string.slice((start = Math.max(0, start - 3)), end));}All it does is start from right to left, grabbing every 3 digits and pushing them into a chunks array.
We then adjust for the scale we're at, and make sure we have enough digits to justify that scale.
Now, here's the key. For each chunk, we process it in this manner:
- Split it into individual integers ("123" becomes [1, 2, 3])
- Add tens word if array item exists
- Add 'and' string after units or tens integer (optional)
- Add hundreds word if array item exists

Complexity of Final Solution
Let n be the number of digits in the input num. We iterate through the string equivalent of num, and examining 3 digits at a time for O(n) linear time complexity. When converting nums to a string of length n, we have O(n) space complexity.
One Pager Cheat Sheet
- You can convert a number to its text equivalent using
number spellinginO(n)time andO(n)space complexity, where n is the number of digits and the number of digits must not exceed100. - We can
convertthenumber123 to astring typeto start our operation. - We need to define
units,tensandscalesfor building the final string, and then divide the number into chunks of 3 digits to account for different scales. - We
iteratethroughnuminO(n)lineartime, andconvertit to astringoflength nforO(n)spacecomplexity.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx print("Nice job, 1/1 tests passed!")# https: //www.quora.com/How-do-I-convert-numbers-to-words-in-Python​ones = [ "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen ",]​twenties = [ "", "", "twenty ", "thirty ", "forty ", "fifty ",That's all we've got! Let's move on to the next tutorial.
If you had any problems with this tutorial, check out the main forum thread here.

