patternjavaMinor
Hacker Rank: Extracting digits from a given number and check for divisibility
Viewed 0 times
numberrankcheckdigitsdivisibilityhackerextractingforandfrom
Problem
The problem in question is a coding challenge from Hackerrank:
Problem Statement
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.
Note
If the same number is repeated twice at different positions, it should
be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at
ones' and tens' position. So for this case, our answer is 3. Division
by 0 is undefined. Input Format
The first line contains T (the number of test cases), followed by T
lines (each containing an integer N).
Constraints
1≤T≤15
0 < N <10 ^ 10
Output Format
For each test case, display the count of digits in N that exactly
divide N in a separate line.
Here's my code:
```
package algorithms.Warmup;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by user1 on 4/2/15.
*/
public class FindDigitsTest {
static Scanner sc = new Scanner(System.in);
private static Long[] findDigits(long num) {
ArrayList digits = new ArrayList<>();
//extract all digits from input number and store in arraylist
while (num > 0) {
long quotient = num / 10;
long remainder = num - (quotient * 10);
num = quotient;
digits.add(remainder);
}
Long[] result = new Long[digits.size()];
result = digits.toArray(result) ;
return result;
}
private static int findCount(Long[] list, long num) {
int count = 0;
for(int i = 0; i al = new ArrayList<>();
//read in number of test cases
System.out.println("Enter number of test cases: ");
int T = sc.nextInt();
//loop through number of test cases
for(int i = 0; i < T; i++) {
System.out.println("Enter the number: ");
Problem Statement
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.
Note
If the same number is repeated twice at different positions, it should
be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at
ones' and tens' position. So for this case, our answer is 3. Division
by 0 is undefined. Input Format
The first line contains T (the number of test cases), followed by T
lines (each containing an integer N).
Constraints
1≤T≤15
0 < N <10 ^ 10
Output Format
For each test case, display the count of digits in N that exactly
divide N in a separate line.
Here's my code:
```
package algorithms.Warmup;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by user1 on 4/2/15.
*/
public class FindDigitsTest {
static Scanner sc = new Scanner(System.in);
private static Long[] findDigits(long num) {
ArrayList digits = new ArrayList<>();
//extract all digits from input number and store in arraylist
while (num > 0) {
long quotient = num / 10;
long remainder = num - (quotient * 10);
num = quotient;
digits.add(remainder);
}
Long[] result = new Long[digits.size()];
result = digits.toArray(result) ;
return result;
}
private static int findCount(Long[] list, long num) {
int count = 0;
for(int i = 0; i al = new ArrayList<>();
//read in number of test cases
System.out.println("Enter number of test cases: ");
int T = sc.nextInt();
//loop through number of test cases
for(int i = 0; i < T; i++) {
System.out.println("Enter the number: ");
Solution
I see that I have too many ArrayLists and Arrays here and there throughout the code, maybe that can be replaced to begin with.
Yes, that would probably be a good idea. In
Returning lists instead of arrays is perfectly acceptable in Java, and is in fact used in most cases (an exception might be made for fixed length arrays in performance critical code, eg coordinates).
So just change your signature from
Misc
Yes, that would probably be a good idea. In
findDigits you internally use an ArrayList, I'm guessing because it's a lot easier to use than an array (which is true, a List is the correct collection type here). But then you transform it to an array, which isn't really needed. Returning lists instead of arrays is perfectly acceptable in Java, and is in fact used in most cases (an exception might be made for fixed length arrays in performance critical code, eg coordinates).
So just change your signature from
Long[] findDigits(long num) to List findDigits(long num), and then use a List in findCount as well.Misc
- declare variables in as small a scope as possible.
scisn't really needed anywhere except inmain, so declare it there.
- don't use short variables names.
scwould be clearer asscannerandalcould beresults.
findDigitscould begetDigits, after all you are not really searching for the digits or computing them (they are right there), just getting them.
findCountcould becountDivisors, and thennumcould bedivisor.
- the question says that you are given an integer, but you use
longinstead.
- your while loop in
findDigitscould be simplified todigits.add(num % 10); num = num / 10;.
Context
StackExchange Code Review Q#85726, answer score: 5
Revisions (0)
No revisions yet.