From 3cbbb5807c2466ccef5469f1b5922517cc5cfd8a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 13 Oct 2024 00:11:00 -0400 Subject: [PATCH] feat: kata/the-latest-clock (#681) * docs: kata description * docs: sync progress * feat: kata/the-latest-clock --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com> --- docs/README.md | 4 ++-- kata/6-kyu/index.md | 1 + kata/6-kyu/the-latest-clock/README.md | 17 +++++++++++++++++ kata/6-kyu/the-latest-clock/main/Kata.java | 16 ++++++++++++++++ .../the-latest-clock/test/LatestClockTest.java | 18 ++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 kata/6-kyu/the-latest-clock/README.md create mode 100644 kata/6-kyu/the-latest-clock/main/Kata.java create mode 100644 kata/6-kyu/the-latest-clock/test/LatestClockTest.java diff --git a/docs/README.md b/docs/README.md index 96ebdf9fb..c08e27425 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-69.0%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) +[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-69.1%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 | 432 | 591 | 219 | 56 | 79 | +| 0 | 1 | 2 | 26 | 48 | 433 | 591 | 219 | 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/6-kyu/index.md b/kata/6-kyu/index.md index cba8888de..e6d74d15d 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -398,6 +398,7 @@ - [The Deaf Rats of Hamelin (2D)](the-deaf-rats-of-hamelin-2d) - [The difference between 11 and 21 in Ping-Pong](the-difference-between-11-and-21-in-ping-pong) - [The Enigma Machine - Part 1: The Plugboard](the-enigma-machine-part-1-the-plugboard) +- [The latest clock](the-latest-clock) - [The lost beginning](the-lost-beginning) - [The Modulo-3 Sequence](the-modulo-3-sequence) - [The Office V - Find a Chair](the-office-v-find-a-chair) diff --git a/kata/6-kyu/the-latest-clock/README.md b/kata/6-kyu/the-latest-clock/README.md new file mode 100644 index 000000000..4ada815e0 --- /dev/null +++ b/kata/6-kyu/the-latest-clock/README.md @@ -0,0 +1,17 @@ +# [The latest clock](https://www.codewars.com/kata/the-latest-clock "https://www.codewars.com/kata/58925dcb71f43f30cd00005f") + +Write a function which receives 4 digits and returns the latest time of day that can be built with those digits. + +The time should be in `HH:MM` format. + +Examples: + +``` +digits: 1, 9, 8, 3 => result: "19:38" +digits: 9, 1, 2, 5 => result: "21:59" (19:25 is also a valid time, but 21:59 is later) +``` + +### Notes + +- Result should be a valid 24-hour time, between `00:00` and `23:59`. +- Only inputs which have valid answers are tested. \ No newline at end of file diff --git a/kata/6-kyu/the-latest-clock/main/Kata.java b/kata/6-kyu/the-latest-clock/main/Kata.java new file mode 100644 index 000000000..418f2b797 --- /dev/null +++ b/kata/6-kyu/the-latest-clock/main/Kata.java @@ -0,0 +1,16 @@ +import static java.util.Comparator.naturalOrder; +import static java.util.stream.Stream.of; + +interface Kata { + static String latestClock(int a, int b, int c, int d) { + int[][] perms = { + {a, b, c, d}, {a, b, d, c}, {a, c, b, d}, {a, c, d, b}, {a, d, b, c}, {a, d, c, b}, + {b, a, c, d}, {b, a, d, c}, {b, c, a, d}, {b, c, d, a}, {b, d, a, c}, {b, d, c, a}, + {c, b, a, d}, {c, b, d, a}, {c, a, b, d}, {c, a, d, b}, {c, d, b, a}, {c, d, a, b}, + {d, b, c, a}, {d, b, a, c}, {d, c, b, a}, {d, c, a, b}, {d, a, b, c}, {d, a, c, b} + }; + return of(perms) + .map(p -> (p[0] == 2 && p[1] < 4 || p[0] < 2) && p[2] < 6 ? "" + p[0] + p[1] + ":" + p[2] + p[3] : "") + .max(naturalOrder()).orElseThrow(); + } +} \ No newline at end of file diff --git a/kata/6-kyu/the-latest-clock/test/LatestClockTest.java b/kata/6-kyu/the-latest-clock/test/LatestClockTest.java new file mode 100644 index 000000000..c0d3e0c34 --- /dev/null +++ b/kata/6-kyu/the-latest-clock/test/LatestClockTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class LatestClockTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 0, 0, 0, 0, 00:00 + 4, 0, 5, 0, 05:40 + 1, 9, 8, 3, 19:38 + 9, 1, 2, 5, 21:59 + 5, 2, 3, 9, 23:59 + """) + void sample(int a, int b, int c, int d, String time) { + assertEquals(time, Kata.latestClock(a, b, c, d)); + } +} \ No newline at end of file