diff --git a/docs/README.md b/docs/README.md index 5fed8e8d0..29f72dfc7 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.2%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) +[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-69.3%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 | 440 | 605 | 227 | 56 | 82 | +| 0 | 1 | 2 | 26 | 48 | 441 | 605 | 227 | 56 | 82 | **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 a2a589c1f..f1a9a414a 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -415,6 +415,7 @@ - [The Walker](the-walker) - [Throw without throwing](throw-without-throwing) - [Tic-Tac-Toe-like table Generator](tic-tac-toe-like-table-generator) +- [Time Math](time-math) - [Tortoise racing](tortoise-racing) - [Traffic Lights - one car](traffic-lights-one-car) - [Transform To Prime](transform-to-prime) diff --git a/kata/6-kyu/time-math/README.md b/kata/6-kyu/time-math/README.md new file mode 100644 index 000000000..6549fe2e8 --- /dev/null +++ b/kata/6-kyu/time-math/README.md @@ -0,0 +1,10 @@ +# [Time Math](https://www.codewars.com/kata/time-math "https://www.codewars.com/kata/5aceae374d9fd1266f0000f0") + +Given two times in hours, minutes, and seconds (ie '15:04:24'), add or subtract them. This is a 24-hour clock. Output should be two digits +for all numbers: hours, minutes, seconds (ie '04:02:09'). + +``` +timeMath('01:24:31', '+', '02:16:05') === '03:40:36' + +timeMath('01:24:31', '-', '02:31:41') === '22:52:50' +``` \ No newline at end of file diff --git a/kata/6-kyu/time-math/main/Solution.java b/kata/6-kyu/time-math/main/Solution.java new file mode 100644 index 000000000..66599128d --- /dev/null +++ b/kata/6-kyu/time-math/main/Solution.java @@ -0,0 +1,15 @@ +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.time.format.ResolverStyle; + +interface Solution { + static String timeMath(String time1, String op, String time2) { + DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss").withResolverStyle(ResolverStyle.LENIENT); + LocalTime t1 = LocalTime.parse(time1, format); + LocalTime t2 = LocalTime.parse(time2, format); + + return op.equals("+") ? + t1.plusHours(t2.getHour()).plusMinutes(t2.getMinute()).plusSeconds(t2.getSecond()).format(format) : + t1.minusHours(t2.getHour()).minusMinutes(t2.getMinute()).minusSeconds(t2.getSecond()).format(format); + } +} \ No newline at end of file diff --git a/kata/6-kyu/time-math/test/SolutionTest.java b/kata/6-kyu/time-math/test/SolutionTest.java new file mode 100644 index 000000000..e822512f3 --- /dev/null +++ b/kata/6-kyu/time-math/test/SolutionTest.java @@ -0,0 +1,27 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 01:24:31, 02:16:05, 03:40:36 + 01:24:31, 22:35:28, 23:59:59 + 00:00:01, 23:59:59, 00:00:00 + 13:48:52, 13:47:53, 03:36:45 + """) + void add(String time1, String time2, String result) { + assertEquals(result, Solution.timeMath(time1, "+", time2)); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + 11:24:31, 11:24:31, 00:00:00 + 11:24:31, 03:15:28, 08:09:03 + 00:00:01, 00:00:02, 23:59:59 + """) + void subtract(String time1, String time2, String result) { + assertEquals(result, Solution.timeMath(time1, "-", time2)); + } +} \ No newline at end of file