Python Nozioni di baseFacile

Sostituisci tutti gli 0 con 1 in un dato numero intero

Guida dettagliata e implementazione Python per il problema "Sostituisci tutti gli 0 con 1 in un dato numero intero".

Dichiarazione del problema

Facile

Scrivi una funzione replace_zeros(n) che accetta un intero positivo n e restituisce un nuovo intero in cui ogni cifra "0" in n è stata sostituita con "1".

Ad esempio, se n = 102030, il risultato dovrebbe essere 112131 (ogni 0 diventa 1).

Vincoli
  • 1 <= n <= 10^9

Esempi

Example 1
Input
replace_zeros(102030)
Output
112131
Explanation

The digits of 102030 are 1,0,2,0,3,0. Replacing each 0 with 1 gives 1,1,2,1,3,1 = 112131.

Example 2
Input
replace_zeros(1000)
Output
1111
Explanation

1,0,0,0 becomes 1,1,1,1 = 1111.

Example 3
Input
replace_zeros(123)
Output
123
Explanation

No zeros in 123, so it remains unchanged.

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.