Python PodstawyŁatwe

Wyszukiwanie Liczba wystąpień x cyfry

Szczegółowy przewodnik i implementacja Python dla problemu „Znajdowanie liczby wystąpień x cyfry”.

Oświadczenie o problemie

Łatwe

Napisz funkcję count_digit(n, x), która pobiera nieujemną liczbę całkowitą n i cyfrę x (0-9) i zwraca liczbę wystąpień cyfry x w n.

Na przykład w liczbie 122333 cyfra 3 pojawia się 3 razy.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla Numbers, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.