HackerRank 'Find Digits' Solution

Martin Kysel · March 10, 2015

Short Problem Definition:

You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.

Find Digits

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Just follow the problem description. The problem can be optimized by creating a map of digits and their counts.

Solution:

#!/usr/bin/py
if __name__ == '__main__':
    t = input()
    for _ in xrange(t):
        a = input()
        count = 0
        for i in list(str(a)):
            if int(i) != 0 and a % int(i) == 0:
                count += 1
        print count

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.