Find Digits

Parag Naik
2 min readFeb 17, 2021

Below is the problem statement

Problem statement
Photo by Hadi Yazdi Aznaveh on Unsplash

Solution:-
A easy hackerrank problem where a number say 124 is given and it is split into single digit and number is divided by each digit ,if the remainder is 0 then the digit is divisor of the number.
Let move towards the code,Let consider a counter variable and initialize it to 0.We will first split the the give number “n” by first converting it into a string and then iterate each element of the string and again converting it back to int and storing each digit in list “num” by using list comprehension.We will then iterate this list and divide the “n” by each digit in list “num” if the remainder is 0 then we will update the counter variable by 1.

def findDigits(n):
counter=0
num=[int(x) for x in str(n)]
for i in num:
try:
if n%i==0:
counter+=1
except:
continue
return counter

Now here there is an edge case which we need to take care of ,if the list of splited number contain a 0 and if we divide any number by 0 it will throw an “ZeroDivisionError” error to avoid this we will use try and except block we will place the divisor checking operation in the try block and in except block we will just use a continue statement so that the for loop will run till the end.at the end of for loop we will return the counter variable.

Conclusion:-
Courage is resistance to fear, mastery of fear, not absence of fear”. Keep hustling and keep learning…(cheers:-PN)

--

--