Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,6 +1,6 @@
|
|||
>>>def josephus(n, k):
|
||||
>>> def josephus(n, k):
|
||||
r = 0
|
||||
for i in xrange(1, n+1):
|
||||
for i in range(1, n+1):
|
||||
r = (r+k)%i
|
||||
return 'Survivor: %d' %r
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,5 @@ def josephus(n, k):
|
|||
return v
|
||||
|
||||
josephus(10, 2)
|
||||
[1, 3, 5, 7, 9, 2, 6, 0, 8, 4]
|
||||
|
||||
josephus(41, 3)[-1]
|
||||
30
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from itertools import compress, cycle
|
||||
def josephus(prisoner, kill, surviver):
|
||||
p = range(prisoner)
|
||||
def josephus(prisoner, kill, survivor):
|
||||
p = list(range(prisoner))
|
||||
k = [0] * kill
|
||||
k[kill-1] = 1
|
||||
s = [1] * kill
|
||||
|
|
@ -10,7 +10,7 @@ def josephus(prisoner, kill, surviver):
|
|||
queue = compress(queue, cycle(s))
|
||||
try:
|
||||
while True:
|
||||
p.append(queue.next())
|
||||
p.append(next(queue))
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
|
|
@ -18,16 +18,13 @@ def josephus(prisoner, kill, surviver):
|
|||
killed = compress(p, cycle(k))
|
||||
try:
|
||||
while True:
|
||||
kil.append(killed.next())
|
||||
kil.append(next(killed))
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
print 'The surviver is: ', kil[-surviver:]
|
||||
print 'The kill sequence is ', kil[:prisoner-surviver]
|
||||
print('The survivor is: ', kil[-survivor:])
|
||||
print('The kill sequence is ', kil[:prisoner-survivor])
|
||||
|
||||
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]
|
||||
josephus(41, 3, 2)
|
||||
|
||||
josephus(5, 2, 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue