Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Josephus-problem/Python/josephus-problem-1.py
Normal file
14
Task/Josephus-problem/Python/josephus-problem-1.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
>>> def j(n, k):
|
||||
p, i, seq = list(range(n)), 0, []
|
||||
while p:
|
||||
i = (i+k-1) % len(p)
|
||||
seq.append(p.pop(i))
|
||||
return 'Prisoner killing order: %s.\nSurvivor: %i' % (', '.join(str(i) for i in seq[:-1]), seq[-1])
|
||||
|
||||
>>> print(j(5, 2))
|
||||
Prisoner killing order: 1, 3, 0, 4.
|
||||
Survivor: 2
|
||||
>>> print(j(41, 3))
|
||||
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15.
|
||||
Survivor: 30
|
||||
>>>
|
||||
11
Task/Josephus-problem/Python/josephus-problem-2.py
Normal file
11
Task/Josephus-problem/Python/josephus-problem-2.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
>>>def josephus(n, k):
|
||||
r = 0
|
||||
for i in xrange(1, n+1):
|
||||
r = (r+k)%i
|
||||
return 'Survivor: %d' %r
|
||||
|
||||
>>> print(josephus(5, 2))
|
||||
Survivor: 2
|
||||
>>> print(josephus(41, 3))
|
||||
Survivor: 30
|
||||
>>>
|
||||
19
Task/Josephus-problem/Python/josephus-problem-3.py
Normal file
19
Task/Josephus-problem/Python/josephus-problem-3.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
def josephus(n, k):
|
||||
a = list(range(1, n + 1))
|
||||
a[n - 1] = 0
|
||||
p = 0
|
||||
v = []
|
||||
while a[p] != p:
|
||||
for i in range(k - 2):
|
||||
p = a[p]
|
||||
v.append(a[p])
|
||||
a[p] = a[a[p]]
|
||||
p = a[p]
|
||||
v.append(p)
|
||||
return v
|
||||
|
||||
josephus(10, 2)
|
||||
[1, 3, 5, 7, 9, 2, 6, 0, 8, 4]
|
||||
|
||||
josephus(41, 3)[-1]
|
||||
30
|
||||
33
Task/Josephus-problem/Python/josephus-problem-4.py
Normal file
33
Task/Josephus-problem/Python/josephus-problem-4.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from itertools import compress, cycle
|
||||
def josephus(prisoner, kill, surviver):
|
||||
p = range(prisoner)
|
||||
k = [0] * kill
|
||||
k[kill-1] = 1
|
||||
s = [1] * kill
|
||||
s[kill -1] = 0
|
||||
queue = p
|
||||
|
||||
queue = compress(queue, cycle(s))
|
||||
try:
|
||||
while True:
|
||||
p.append(queue.next())
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
kil=[]
|
||||
killed = compress(p, cycle(k))
|
||||
try:
|
||||
while True:
|
||||
kil.append(killed.next())
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
print 'The surviver is: ', kil[-surviver:]
|
||||
print 'The kill sequence is ', kil[:prisoner-surviver]
|
||||
|
||||
josephus(41,3,2)
|
||||
The surviver is: [15, 30]
|
||||
The kill sequence is [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34]
|
||||
josephus(5,2,1)
|
||||
The surviver is: [2]
|
||||
The kill sequence is [1, 3, 0, 4]
|
||||
Loading…
Add table
Add a link
Reference in a new issue