-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(5-kyu): kata/josephus-survivor (#490)
- Loading branch information
1 parent
4fb324d
commit a8a7e02
Showing
5 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
kata/5-kyu/josephus-survivor/test/JosephusSurvivorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |