Last active
October 29, 2016 17:24
-
-
Save anddam/c2711c52160f7cba93ef1db9f6c9aa04 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def josephus_problem_iterate(soldiers): | |
"Iterative solution to Josephus' problem" | |
while len(soldiers) > 1: | |
start = 2 if len(soldiers) % 2 else 0 | |
soldiers = soldiers[start::2] | |
return soldiers | |
def josephus_problem_recurse(soldiers): | |
"Recursive solution to Josephus' problem" | |
if len(soldiers) <= 1: | |
return soldiers | |
start = 2 if len(soldiers) % 2 else 0 | |
return josephus_problem_recurse(soldiers[start::2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment