From 87a5c201b83937e9cf3be339259d23b2aabe62b6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:27:34 -0400 Subject: [PATCH] feat: kata/number-to-digit-tiers (#642) --- docs/README.md | 4 +-- kata/7-kyu/index.md | 1 + kata/7-kyu/number-to-digit-tiers/README.md | 13 ++++++++++ .../number-to-digit-tiers/main/Solution.java | 8 ++++++ .../test/SolutionTest.java | 26 +++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 kata/7-kyu/number-to-digit-tiers/README.md create mode 100644 kata/7-kyu/number-to-digit-tiers/main/Solution.java create mode 100644 kata/7-kyu/number-to-digit-tiers/test/SolutionTest.java diff --git a/docs/README.md b/docs/README.md index 066090776..c2fa2fd0d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ # Codewars Handbook ☕️🚀 [![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook) -[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.5%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) +[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.6%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) [![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml) [![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook) [![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook) @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 48 | 430 | 577 | 214 | 56 | 79 | +| 0 | 1 | 2 | 26 | 48 | 430 | 578 | 214 | 56 | 79 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index d638f4d68..4a625e523 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -353,6 +353,7 @@ - [Number of People in the Bus](number-of-people-in-the-bus) - [Number of Rectangles in a Grid](number-of-rectangles-in-a-grid) - [Number-Star ladder](number-star-ladder) +- [Number to digit tiers](number-to-digit-tiers) - [Numbers in strings](numbers-in-strings) - [Numbers Which Sum of Powers of Its Digits Is The Same Number](numbers-which-sum-of-powers-of-its-digits-is-the-same-number) - [Numbers with d occurrences of digit d](numbers-with-d-occurences-of-digit-d) diff --git a/kata/7-kyu/number-to-digit-tiers/README.md b/kata/7-kyu/number-to-digit-tiers/README.md new file mode 100644 index 000000000..fa328be3f --- /dev/null +++ b/kata/7-kyu/number-to-digit-tiers/README.md @@ -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]` \ No newline at end of file diff --git a/kata/7-kyu/number-to-digit-tiers/main/Solution.java b/kata/7-kyu/number-to-digit-tiers/main/Solution.java new file mode 100644 index 000000000..1c1159f90 --- /dev/null +++ b/kata/7-kyu/number-to-digit-tiers/main/Solution.java @@ -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); + } +} \ No newline at end of file diff --git a/kata/7-kyu/number-to-digit-tiers/test/SolutionTest.java b/kata/7-kyu/number-to-digit-tiers/test/SolutionTest.java new file mode 100644 index 000000000..e811686d9 --- /dev/null +++ b/kata/7-kyu/number-to-digit-tiers/test/SolutionTest.java @@ -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 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)); + } +} \ No newline at end of file