Python BasicsЛегко

Finding Number of times x digit occurs

Detailed guide and Python implementation for the 'Finding Number of times x digit occurs' problem.

Постановка задачи

Легко

Write a function count_digit(n, x) that takes a non-negative integer n and a digit x (0-9), and returns the number of times digit x appears in n.

For example, in the number 122333, the digit 3 appears 3 times.

Ограничения
  • 0 <= n <= 10^9
  • 0 <= x <= 9

Примеры

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?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

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

Открыть в редакторе
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

Рекомендуемые ресурсы Python

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Python Regex

Освойте регулярные выражения (Regex) в Python. Научитесь искать, сопоставлять, разбивать и заменять строковые данные с помощью встроенной библиотеки re.

Посмотреть ресурс
Практическое руководство

Как найти длину списка в Python

Узнайте, как найти длину списка в Python с помощью функции len(). Поймите временную сложность O(1) и количество проверок.

Посмотреть ресурс
Шпаргалка

Шаблоны регулярных выражений Python (перемодуль) Шпаргалка

Справочное руководство по регулярным выражениям Python. Изучите шаблоны сопоставления, поиска, поиска, подзаголовков и основные шаблоны регулярных выражений.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс