Python Nozioni di baseFacile

Trovare Numero di volte in cui si verifica la cifra x

Guida dettagliata e implementazione Python per il problema "Ricerca del numero di volte in cui si verifica una cifra".

Dichiarazione del problema

Facile

Scrivi una funzione count_digit(n, x) che accetta un intero non negativo n e una cifra x (0-9) e restituisce il numero di volte in cui la cifra x appare in n.

Ad esempio, nel numero 122333, la cifra 3 appare 3 volte.

Vincoli
  • 0 <= n <= 10^9
  • 0 <= x <= 9

Esempi

Example 1
Input
count_digit(122333, 3)
Output
3
Explanation

The digits of 122333 are 1,2,2,3,3,3. The digit 3 appears 3 times.

Example 2
Input
count_digit(1000, 0)
Output
3
Explanation

The digits of 1000 are 1,0,0,0. The digit 0 appears 3 times.

Example 3
Input
count_digit(12345, 6)
Output
0
Explanation

The digit 6 does not appear in 12345.

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.