from math import log, floor # for use in Kill3 """ Killing Circle Problem Ascertain the surviving spot and the killing order recursively. Inputs: n = num people in circle, people in circle would be represented starting at 0 k = position of person killed in sequence (eg: k=3 ~> 1 & 2 skipped, 3rd is killed, 4 & 5 skipped, 6th killed etc.) """ def kill1(n, k): p, i, seq = list(range(n)), 0, [] while p: i = (i + k - 1) % len(p) seq.append(p.pop(i)) return 'Killing order: %s.\nSurvivor: %i\n' % (', '.join(str(i) for i in seq[:-1]), seq[-1]) # a slightly faster method to ascertain the surviver without keeping track of killing order. def kill2(n, k): r = 0 for i in range(1, n + 1): r = (r + k) % i return r # fastest mathod to arrive at answer mathematically (instead of algorithmically) # assumes k==2 def kill3(n): r = int(2 * (n - 2 ** (floor(log(n, 2))))) return r
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question