Python Nozioni di baseFacile

Calcolare il numero di cifre in un numero intero

Guida dettagliata e implementazione Python per il problema "Calcola il numero di cifre in un numero intero".

Dichiarazione del problema

Facile

Scrivi una funzione count_digits(n) che accetta un numero intero n e restituisce il numero di cifre nel suo valore assoluto. Ad esempio, sia 123 che -123 hanno 3 cifre.

Nota: il numero 0 ha 1 cifra.

Vincoli
  • -10^9 <= n <= 10^9

Esempi

Example 1
Input
count_digits(12345)
Output
5
Explanation

12345 has digits 1, 2, 3, 4, 5 — that's 5 digits.

Example 2
Input
count_digits(-987)
Output
3
Explanation

Ignoring the sign, 987 has 3 digits.

Example 3
Input
count_digits(0)
Output
1
Explanation

The number 0 has exactly 1 digit.

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.