Back to Practice Dashboard
Python BasicsEasy

Convert digit/number to words

Learn how to solve the 'Convert digit/number to words' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function number_to_words(n) that takes a non-negative integer n and returns a string where each digit is converted to its English word, separated by spaces.

Digit-word mapping: 0='Zero', 1='One', 2='Two', 3='Three', 4='Four', 5='Five', 6='Six', 7='Seven', 8='Eight', 9='Nine'.

For example, 123 → 'One Two Three'.

Constraints
  • 0 <= n <= 10^9

Examples

Example 1
Input
number_to_words(123)
Output
'One Two Three'
Explanation

1 → 'One', 2 → 'Two', 3 → 'Three'. Joined with spaces.

Example 2
Input
number_to_words(405)
Output
'Four Zero Five'
Explanation

4 → 'Four', 0 → 'Zero', 5 → 'Five'.

Example 3
Input
number_to_words(0)
Output
'Zero'
Explanation

Single digit 0 → 'Zero'.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor