-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kata/number-to-digit-tiers (#642)
- Loading branch information
1 parent
55ed708
commit 87a5c20
Showing
5 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# [Number to digit tiers](https://www.codewars.com/kata/number-to-digit-tiers "https://www.codewars.com/kata/586bca7fa44cfc833e00005c") | ||
|
||
Create a function that takes a number and returns an array of strings containing the number cut off at each digit. | ||
|
||
### Examples | ||
|
||
* `420` should return `["4", "42", "420"]` | ||
* `2017` should return `["2", "20", "201", "2017"]` | ||
* `2010` should return `["2", "20", "201", "2010"]` | ||
* `4020` should return `["4", "40", "402", "4020"]` | ||
* `80200` should return `["8", "80", "802", "8020", "80200"]` | ||
|
||
PS: The input is guaranteed to be an integer in the range `[0, 1000000]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import static java.util.stream.IntStream.range; | ||
|
||
interface Solution { | ||
static String[] createArrayOfTiers(int num) { | ||
var s = num + ""; | ||
return range(0, s.length()).mapToObj(i -> s.substring(0, i + 1)).toArray(String[]::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class SolutionTest { | ||
private static Stream<Arguments> testData() { | ||
return Stream.of( | ||
Arguments.of(0, new String[]{"0"}), | ||
Arguments.of(6, new String[]{"6"}), | ||
Arguments.of(420, new String[]{"4", "42", "420"}), | ||
Arguments.of(2017, new String[]{"2", "20", "201", "2017"}), | ||
Arguments.of(2010, new String[]{"2", "20", "201", "2010"}), | ||
Arguments.of(4020, new String[]{"4", "40", "402", "4020"}), | ||
Arguments.of(80200, new String[]{"8", "80", "802", "8020", "80200"}) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("testData") | ||
void sample(int num, String[] expected) { | ||
assertArrayEquals(expected, Solution.createArrayOfTiers(num)); | ||
} | ||
} |