-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteNumberInExpandedForm.java
44 lines (37 loc) · 1.29 KB
/
WriteNumberInExpandedForm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// https://www.codewars.com/kata/5842df8ccbd22792a4000245
public class WriteNumberInExpandedForm {
private final int input;
public WriteNumberInExpandedForm(int input) {
this.input = input;
}
public String solution() {
List<String> splittedNumber = new LinkedList<>(
Arrays.asList(
String.valueOf(input).split("")
)
);
int splittedNumbers = splittedNumber.size() - 1;
List<String> numbers = new LinkedList<>();
for (int i = 0; i < splittedNumber.size(); i++) {
int digit = Integer.parseInt(splittedNumber.get(i));
if (digit != 0) {
numbers.add(
String.format(
("%d%s"),
digit,
String.join(
"",
Collections.nCopies(splittedNumbers - i, "0")
)
)
);
}
}
return String.join(" + ", numbers);
}
}