Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions kata/7-kyu/no-ifs-no-buts/README.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions kata/7-kyu/no-ifs-no-buts/main/NoIfsNoButs.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions kata/7-kyu/no-ifs-no-buts/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}