Konnichiwa fellow programmers and welcome to another algorithm session!π
In this article, I'll be describing how I solved the
find digits
Challenge on HackerRank using Python 3.
This challenge is part of the Implementation
challenges in the Algorithm
section on HackerRank.
If this is your first time reading my data structures and algorithms article, consider reading more in my Algos in Plain English series.π
Problem π²
An integer d is a divisor of an integer n if the remainder of n Γ· d = 0. 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.
Example
n = 124
Check whether 1, 2 and 4 are divisors of 124. All 3 numbers divide evenly into 124 so return 3.
n = 10
Check whether 1 and 0 are divisors of 10. 1 is but 0 isn't. Return 1.
Function Description
Complete the findDigits
function in the editor below.
findDigits has the following parameter(s):
- int n: the value to analyze
Returns
- int: the number of digits in n that are divisors of n
Check out the original challenge on HackerRank for more info on input constraints and other sample inputs.
Solution π
In plain English, a digit d is a divisor of a number, n, if there's no remainder, i.e. n Γ· d = 0.
Pretty straightforward hereπ
Let's write down the steps we'll use to solve this problem.
# First thing to do is iterate over the digits in n.
# Then check the divisor logic for each digit.
# Finally, update the number of divisors based on the result of the divisor logic.
Step 1οΈβ£: Initialising variables π.
As always. we'll create variables to track and better understand the control flow and logic.
We track the current number of divisors in the divisors
variable.
Step 2οΈβ£: Iterate over the digits in n
.
- The first thing you'll notice in this problem is the need to iterate through all digits in
n
, however, since theint
object (as far as python is concerned) is not iterable, we'll need to convert it to itsstring
representation.def findDigits(n): # Write your code here stringDigits = str(n) divisors = 0
- Now we iterate over the digits while running the
divisor
logic on each one.But here's the catch! you can't divide the number
n
by a string.
A workaround is to map the string digits to int so they can be divisible.π€
def findDigits(n):
# Write your code here
stringDigits = str(n)
divisors = 0
for digit in map(int, stringDigits):
Now you can access all the integer digits from their string representation.
Step 3οΈβ£: Return the number of divisors using the 'divisor' logic β.
We need to handle a special case when the digit is 0, so as to avoid a ZeroDivisionError
. In this case, we simply continue the iteration without carrying out any action.
def findDigits(n):
# Write your code here
stringDigits = str(n)
divisors = 0
for digit in map(int, stringDigits):
if digit == 0:
continue
if n % digit == 0:
divisors += 1
return divisors
That's it! Thanks for reading! π
Hope this article helped with understanding this challenge. Happy Coding!π¨π½βπ»
Nonsocchiπ₯·π½