diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index f1180483..6d276a9a 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -363,6 +363,7 @@ - [Name That Number!](name-that-number "579ba41ce298a73aaa000255") - [Nice Array](nice-array "59b844528bcb7735560000a0") - [Nickname Generator](nickname-generator "593b1909e68ff627c9000186") +- [No ifs no buts](no-ifs-no-buts "592915cc1fad49252f000006") - [Nth power rules them all!](nth-power-rules-them-all "58aed2cafab8faca1d000e20") - [Nth Smallest Element (Array Series #4)](nth-smallest-element-array-series-number-4 "5a512f6a80eba857280000fc") - [Null](null "5dd73cd4cf95d3000194617d") diff --git a/kata/7-kyu/no-ifs-no-buts/README.md b/kata/7-kyu/no-ifs-no-buts/README.md new file mode 100644 index 00000000..41ff1c1f --- /dev/null +++ b/kata/7-kyu/no-ifs-no-buts/README.md @@ -0,0 +1,15 @@ +# [No ifs no buts](https://www.codewars.com/kata/no-ifs-no-buts "https://www.codewars.com/kata/592915cc1fad49252f000006") + +Write a function that accepts two numbers `a` and `b` and returns whether `a` is smaller than, bigger than, or equal to `b`, as a string. + +``` +(5, 4) ---> "5 is greater than 4" +(-4, -7) ---> "-4 is greater than -7" +(2, 2) ---> "2 is equal to 2" +``` + +There is only one problem... + +You cannot use `if` statements, and you cannot use the ternary operator `? :`. + +In fact the word `if` and the character `?` are not allowed in your code. \ No newline at end of file diff --git a/kata/7-kyu/no-ifs-no-buts/main/NoIfsNoButs.java b/kata/7-kyu/no-ifs-no-buts/main/NoIfsNoButs.java new file mode 100644 index 00000000..98dc0811 --- /dev/null +++ b/kata/7-kyu/no-ifs-no-buts/main/NoIfsNoButs.java @@ -0,0 +1,9 @@ +interface NoIfsNoButs { + static String noIfsNoButs(int a, int b) { + return a + " is " + switch (Integer.compare(a, b)) { + case -1 -> "smaller than "; + case 1 -> "greater than "; + default -> "equal to "; + } + b; + } +} \ No newline at end of file diff --git a/kata/7-kyu/no-ifs-no-buts/test/SolutionTest.java b/kata/7-kyu/no-ifs-no-buts/test/SolutionTest.java new file mode 100644 index 00000000..1dabc8fb --- /dev/null +++ b/kata/7-kyu/no-ifs-no-buts/test/SolutionTest.java @@ -0,0 +1,20 @@ +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 = """ + -3, 2, -3 is smaller than 2 + 1, 2, 1 is smaller than 2 + 45, 51, 45 is smaller than 51 + 1, 1, 1 is equal to 1 + 100, 100, 100 is equal to 100 + 20, 19, 20 is greater than 19 + 100, 80, 100 is greater than 80 + """) + void sample(int a, int b, String phrase) { + assertEquals(phrase, NoIfsNoButs.noIfsNoButs(a, b)); + } +} \ No newline at end of file