Last active
December 29, 2015 10:39
-
-
Save daboross/7658940 to your computer and use it in GitHub Desktop.
Josephus http://danvk.org/josephus.html
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
#!/usr/bin/env python3 | |
import sys | |
size, every_x, current = [int(x) for x in sys.argv[1:]] | |
alive, till_kill = [True] * size, 0 | |
while True: | |
if alive[current]: | |
if sum(alive) == 1: | |
break | |
elif till_kill == 0: | |
alive[current] = False | |
till_kill = every_x - 1 | |
else: | |
till_kill -= 1 | |
current += 1 if current < size - 1 else 1 - size | |
print("Josephus should be at position {} to survive.".format(current)) |
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
#!/usr/bin/env python3 | |
import sys | |
s, k, c = [int(x) for x in sys.argv[1:]] | |
a, tn = [True] * s, 0 | |
while True: | |
if a[c]: | |
if sum(a) == 1: break | |
elif tn == 0: a[c] = False; tn = k - 1 | |
else: tn -= 1 | |
c += 1 if c < s - 1 else 1 - s | |
print("Josephus should be at position {} to survive.".format(c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment