Interview question: Determine the number of numerical digits in a string
If you are intending to become a professional Python programmer then it is highly likely that you will need to go on a coding interview at some point in your quest. String operations are quite common interview questions, so it is a good idea to know the algorithms that solve the problems you will be given during an interview.
One such question is how to determine the number of numerical digits in a string. In this blog post I have presented two methods to solve the problem:-
- Use a list of digits to accompany the string.
- Use Python’s inbuilt function to determine if the character is a digit.
The psuedocode for using the first method of determining if a character in a string is a digit is:-
- Define the list, digit_lst, which is each digit from 0 to 9 in string format.
- Define the string that will need to be checked.
- Define the function, count_num, which accepts the variables, string and digit_lst, as input.
- In the function, define the variable, count, and initialise it to 0.
- In the function, create a for loop that will iterate through the length of the string. If the iterated element of the string is in digit_list then…