loading...

re: Daily Challenge #62 - Josephus Survivor VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Correct me if I'm wrong. Your test case for n = 7, k = 3 checks for an expected result of 3, but based on the same example above, the results shou...
 

No its right, what the Josephus permutation is doing is telling us which solider will survive after all eliminations have happened. The example above gives us soldier 4, which, in a standard array would be position 3 in the array as arrays start at 0 and count up.

Example:

In Go, if you had a slice that represented the group of solders in the example, you would get the index of soldier in the slice that will survive the elimination.

If you wanted to print a more human readable output using the result of the Josephus permutation, you would simply do something like:

soldiers := []string{"Bob", "Mark", "DatBoi", "John", "Josephus", "Donald", "Melvin"}
survivor := josephus.Survivor(len(soldiers), 3)
fmt.Println("Soldier %d: '%s' - survived!", survivor + 1, soldiers[survivor])

Which would produce the output:

"Soldier 4: 'John' - survived!"

I see, if the array starts from 0, the results from your test cases would make sense.

Thanks for clarifying!

Thank you for the question! In the future if my test cases look different than the examples given I will clarify the reason in my original post.

Code of Conduct Report abuse