Skip to content

Commit

Permalink
feat(5-kyu): kata/josephus-survivor (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
ParanoidUser committed Jan 28, 2024
1 parent 4fb324d commit a8a7e02
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 42 | 416 | 559 | 206 | 55 | 79 |
| 0 | 1 | 2 | 26 | 43 | 416 | 559 | 206 | 55 | 79 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
1 change: 1 addition & 0 deletions kata/5-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [Is the King in check ?](is-the-king-in-check)
# J
- [Josephus Permutation](josephus-permutation)
- [Josephus Survivor](josephus-survivor)
# L
- [Land perimeter](land-perimeter)
- [Last digit of a large number](last-digit-of-a-large-number)
Expand Down
27 changes: 27 additions & 0 deletions kata/5-kyu/josephus-survivor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# [Josephus Survivor](https://www.codewars.com/kata/josephus-survivor "https://www.codewars.com/kata/555624b601231dc7a400017a")

In this kata you have to correctly return who is the "survivor", ie: the last element of
a <a href="http://www.codewars.com/kata/josephus-permutation/" target="_blank" title="Josephus sequence">Josephus
permutation</a>.

Basically you have to assume that n people are put into a circle and that they are eliminated in steps of k elements,
like this:

```
n=7, k=3 => means 7 people in a circle
one every 3 is eliminated until one remains
[1,2,3,4,5,6,7] - initial sequence
[1,2,4,5,6,7] => 3 is counted out
[1,2,4,5,7] => 6 is counted out
[1,4,5,7] => 2 is counted out
[1,4,5] => 7 is counted out
[1,4] => 5 is counted out
[4] => 1 counted out, 4 is the last element - the survivor!
```

The above link about the "base" kata description will give you a more thorough insight about the origin of this kind of
permutation, but basically that's all that there is to know to solve this kata.

**Notes and tips:** using the solution to the other kata to check your function may be helpful, but as much larger
numbers will be used, using an array/list to compute the number of the survivor may be too slow; you may assume that
both n and k will always be >=1.
5 changes: 5 additions & 0 deletions kata/5-kyu/josephus-survivor/main/JosephusSurvivor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface JosephusSurvivor {
static int josephusSurvivor(int n, int k) {
return n > 1 ? (josephusSurvivor(n - 1, k--) + k) % n + 1 : 1;
}
}
14 changes: 14 additions & 0 deletions kata/5-kyu/josephus-survivor/test/JosephusSurvivorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class JosephusSurvivorTest {
@Test
void sample() {
assertEquals(4, JosephusSurvivor.josephusSurvivor(7, 3));
assertEquals(10, JosephusSurvivor.josephusSurvivor(11, 19));
assertEquals(28, JosephusSurvivor.josephusSurvivor(40, 3));
assertEquals(13, JosephusSurvivor.josephusSurvivor(14, 2));
assertEquals(100, JosephusSurvivor.josephusSurvivor(100, 1));
}
}

0 comments on commit a8a7e02

Please sign in to comment.