Python Nozioni di baseFacile

Converti cifra/numero in parole

Guida dettagliata e implementazione Python per il problema "Converti cifre/numeri in parole".

Dichiarazione del problema

Facile

Scrivi una funzione number_to_words(n) che accetta un intero non negativo n e restituisce una stringa in cui ogni cifra viene convertita nella sua parola inglese, separata da spazi.

Mappatura cifre-parole: 0='Zero', 1='Uno', 2='Due', 3='Tre', 4='Quattro', 5='Cinque', 6='Sei', 7='Sette', 8='Otto', 9='Nove'.

Ad esempio, 123 → 'Uno Due Tre'.

Vincoli
  • 0 <= n <= 10^9

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.