Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kata/odder-than-the-rest #673

Merged
merged 2 commits into from
Oct 11, 2024
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 @@ -368,6 +368,7 @@
- [O's and X's adjudicator](os-and-xs-adjudicator)
- [Odd March Bits 8 bits](odd-march-bits-8-bits-1)
- [Odd or Even?](odd-or-even)
- [Odder Than the Rest](odder-than-the-rest)
- [Odd-Even String Sort](odd-even-string-sort)
- [Ones and Zeros](ones-and-zeros)
- [Ordered Count of Characters](ordered-count-of-characters)
Expand Down
13 changes: 13 additions & 0 deletions kata/7-kyu/odder-than-the-rest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# [Odder Than the Rest](https://www.codewars.com/kata/odder-than-the-rest "https://www.codewars.com/kata/5983cba828b2f1fd55000114")

Create a method that takes an array/list as an input, and outputs the index at which the sole odd number is located. This method should work
with arrays with negative numbers. If there are no odd numbers in the array, then the method should output `-1`.

Examples:

```
oddOne([2, 4, 6, 7, 10]) // => 3
oddOne([2, 16, 98, 10, 13, 78]) // => 4
oddOne([4, -8, 98, -12, -7, 90, 100]) // => 4
oddOne([2, 4, 6, 8]) // => -1
```
7 changes: 7 additions & 0 deletions kata/7-kyu/odder-than-the-rest/main/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import static java.util.stream.IntStream.range;

interface Solution {
static int oddOne(int[] arr) {
return range(0, arr.length).filter(i -> arr[i] % 2 != 0).findFirst().orElse(-1);
}
}
13 changes: 13 additions & 0 deletions kata/7-kyu/odder-than-the-rest/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sample() {
assertEquals(3, Solution.oddOne(new int[]{2, 4, 6, 7, 10}));
assertEquals(4, Solution.oddOne(new int[]{2, 16, 98, 10, 13, 78}));
assertEquals(4, Solution.oddOne(new int[]{4, -8, 98, -12, -7, 90, 100}));
assertEquals(-1, Solution.oddOne(new int[]{2, 4, 6, 8}));
}
}