Skip to content

Commit b9d50e7

Browse files
committed
1 parent ee09887 commit b9d50e7

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
- [Name That Number!](name-that-number "579ba41ce298a73aaa000255")
364364
- [Nice Array](nice-array "59b844528bcb7735560000a0")
365365
- [Nickname Generator](nickname-generator "593b1909e68ff627c9000186")
366+
- [No ifs no buts](no-ifs-no-buts "592915cc1fad49252f000006")
366367
- [Nth power rules them all!](nth-power-rules-them-all "58aed2cafab8faca1d000e20")
367368
- [Nth Smallest Element (Array Series #4)](nth-smallest-element-array-series-number-4 "5a512f6a80eba857280000fc")
368369
- [Null](null "5dd73cd4cf95d3000194617d")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# [No ifs no buts](https://www.codewars.com/kata/no-ifs-no-buts "https://www.codewars.com/kata/592915cc1fad49252f000006")
2+
3+
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.
4+
5+
```
6+
(5, 4) ---> "5 is greater than 4"
7+
(-4, -7) ---> "-4 is greater than -7"
8+
(2, 2) ---> "2 is equal to 2"
9+
```
10+
11+
There is only one problem...
12+
13+
You cannot use `if` statements, and you cannot use the ternary operator `? :`.
14+
15+
In fact the word `if` and the character `?` are not allowed in your code.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface NoIfsNoButs {
2+
static String noIfsNoButs(int a, int b) {
3+
return a + " is " + switch (Integer.compare(a, b)) {
4+
case -1 -> "smaller than ";
5+
case 1 -> "greater than ";
6+
default -> "equal to ";
7+
} + b;
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
-3, 2, -3 is smaller than 2
10+
1, 2, 1 is smaller than 2
11+
45, 51, 45 is smaller than 51
12+
1, 1, 1 is equal to 1
13+
100, 100, 100 is equal to 100
14+
20, 19, 20 is greater than 19
15+
100, 80, 100 is greater than 80
16+
""")
17+
void sample(int a, int b, String phrase) {
18+
assertEquals(phrase, NoIfsNoButs.noIfsNoButs(a, b));
19+
}
20+
}

0 commit comments

Comments
 (0)