Hackerrank - Find Digits Solution
1 min read

# Hackerrank - Find Digits Solution

An integer  is a divisor of an integer  if the remainder of .

Given an integer, for each digit that makes up the integer determine whether it is a divisor. Count the number of divisors occurring within the integer.

Note: Each digit is considered to be unique, so each occurrence of the same digit should be counted (e.g. for ,  is a divisor of  each time it occurs so the answer is ).

Function Description

Complete the findDigits function in the editor below. It should return an integer representing the number of digits of  that are divisors of .

findDigits has the following parameter(s):

• n: an integer to analyze

Input Format

The first line is an integer, , indicating the number of test cases.
The  subsequent lines each contain an integer, .

Constraints

Output Format

For every test case, count the number of digits in  that are divisors of . Print each answer on a new line.

Sample Input

2
12
1012


Sample Output

2
3


Explanation

The number  is broken into two digits,  and . When  is divided by either of those two digits, the remainder is  so they are both divisors.

The number  is broken into four digits, , , , and .  is evenly divisible by its digits , , and , but it is not divisible by  as division by zero is undefined.

### Solution in Python

from collections import Counter
def findDigits(n):
c=0
for x,y in Counter(str(n)).items():
if int(x) and not n%int(x):
c+=y
return c

for _ in range(int(input())):
print(findDigits(int(input())))


Solution using list comprehension

from collections import Counter
def findDigits(n):
return sum(y for x,y in Counter(str(n)).items() if int(x) and not n%int(x))
for _ in range(int(input())):
print(findDigits(int(input())))


# Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×