Python Basics簡単

Convert digit/number to words

Detailed guide and Python implementation for the 'Convert digit/number to words' problem.

問題提起

簡単

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'.

制約
  • 0 <= n <= 10^9

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?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。