Count a specific digit in data

Problem Statement

Consider that we are implementing an encryption algorithm, where we need to get the number of occurrences of a given digit (numeric character) in the data (a string).

Write a Python function that takes the digit, and data, as arguments, and return the number of occurrences of the digit in the data.

Input

The first line of input consists of the data, a string.

The second line of input consists of the digit, whose number of appearances we need to find.

Output

Print the number of occurrences of the digit in the data.

As is

The boilerplate code is already there to read the input from user, in the main() function, and to print the value returned by the countDigit() function. Do not edit this code.

To do ✍

countDigit() has two parameters: data, digit. Complete the function that counts the occurrences of given digit in the data.

Example 1

Test input

1245125336875555142
5

Test output

6

Explanation

The digit 5 has occurred in the data 1245125336875555142 for 6 times. Hence, output is 6.

Example 2

Test input

11141255541
9

Test output

0

Explanation

There is no occurrence of the digit 9 in the given data. Hence output is 0.

Useful References

Python Program

# Complete the following function
def countDigit(data, digit):
    count = 0


    
    return count

# Boilerplate code - Do not edit the following
def main():
    data = input()
    digit = input()
    print(countDigit(data, digit))

if __name__ == "__main__":
    main()

Input and Outputs 1

1245125336875555142
1
3

Input and Outputs 2

5251
8
0

Input and Outputs 3

8946123212798454646
6
3
# Complete the following function
def countDigit(data, digit):
    count = 0
    # For each character in the given data
    for char in data:
        # If character in the data equals digit,
        # increment the count by one
        if char == digit:
            count += 1
    
    return count

# Boilerplate code - Do not edit the following
def main():
    data = input()
    digit = input()
    print(countDigit(data, digit))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍