Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
37
Task/Josephus-problem/AWK/josephus-problem.awk
Normal file
37
Task/Josephus-problem/AWK/josephus-problem.awk
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# syntax: GAWK -f JOSEPHUS_PROBLEM.AWK
|
||||
# converted from PL/I
|
||||
BEGIN {
|
||||
main(5,2,1)
|
||||
main(41,3,1)
|
||||
main(41,3,3)
|
||||
exit(0)
|
||||
}
|
||||
function main(n,k,s, dead,errors,found,i,killed,nn,p,survived) {
|
||||
# n - number of prisoners
|
||||
# k - kill every k'th prisoner
|
||||
# s - number of survivors
|
||||
printf("\nn=%d k=%d s=%d\n",n,k,s) # show arguments
|
||||
if (s > n) { print("s>n"); errors++ }
|
||||
if (k <= 0) { print("k<=0"); errors++ }
|
||||
if (errors > 0) { return(0) }
|
||||
nn = n # wrap around boundary
|
||||
p = -1 # start here
|
||||
while (n != s) { # until survivor count is met
|
||||
found = 0 # start looking
|
||||
while (found != k) { # until we have the k-th prisoner
|
||||
if (++p == nn) { p = 0 } # wrap around
|
||||
if (dead[p] != 1) { found++ } # if prisoner is alive increment found
|
||||
}
|
||||
dead[p] = 1 # kill the unlucky one
|
||||
killed = killed p " " # build killed list
|
||||
n-- # reduce size of circle
|
||||
}
|
||||
for (i=0; i<=nn-1; i++) {
|
||||
if (dead[i] != 1) {
|
||||
survived = survived i " " # build survivor list
|
||||
}
|
||||
}
|
||||
printf("killed: %s\n",killed)
|
||||
printf("survived: %s\n",survived)
|
||||
return(1)
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ T pop(T)(ref T[] items, in size_t i) pure {
|
|||
return aux;
|
||||
}
|
||||
|
||||
string josephus(in int n, in int k) /*pure*/ {
|
||||
string josephus(in int n, in int k) pure {
|
||||
auto p = n.iota.array;
|
||||
int i;
|
||||
int[] seq;
|
||||
|
|
|
|||
1
Task/Josephus-problem/Forth/josephus-problem.fth
Normal file
1
Task/Josephus-problem/Forth/josephus-problem.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
: josephus 0 1 begin dup 41 <= while swap 3 + over mod swap 1+ repeat drop ;
|
||||
36
Task/Josephus-problem/Groovy/josephus-problem.groovy
Normal file
36
Task/Josephus-problem/Groovy/josephus-problem.groovy
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
int[] Josephus (int size, int kill, int survivors) {
|
||||
// init user pool
|
||||
def users = new int[size];
|
||||
|
||||
// give initial values such that [0] = 1 (first person) [1] = 2 (second person) etc
|
||||
users.eachWithIndex() {obj, i -> users[i] = i + 1};
|
||||
|
||||
// keep track of which person we are on (ranging from 1 to kill)
|
||||
def person = 1;
|
||||
|
||||
// keep going until we have the desired number of survivors
|
||||
while (users.size() > survivors)
|
||||
{
|
||||
// for each person, if they are the kill'th person, set them to -1 to show eliminated
|
||||
users.eachWithIndex() {obj, i ->
|
||||
if (person++ % kill == 0) {
|
||||
users[i] = -1;
|
||||
}
|
||||
|
||||
// if person overflowed kill then reset back to 1
|
||||
if (person > kill) {person = 1;}
|
||||
}
|
||||
|
||||
// clear out all eliminated persons
|
||||
users = users.findAll{w -> w >= 0};
|
||||
}
|
||||
|
||||
// resulting set is the safe positions
|
||||
return users;
|
||||
}
|
||||
|
||||
// Run some test cases
|
||||
|
||||
println "Final survivor for n = 10201 and k = 17: " + Josephus(10201,17,1)[0];
|
||||
|
||||
println "4 safe spots for n = 10201 and k = 17: " + Josephus(10201,17,4);
|
||||
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function josephus(n, k, m)
|
||||
local positions={}
|
||||
for i=1,n do
|
||||
table.insert(positions, i-1)
|
||||
end
|
||||
local i,j=1,1
|
||||
local s='Execution order: '
|
||||
while #positions>m do
|
||||
if j==k then
|
||||
s=s .. positions[i] .. ', '
|
||||
table.remove(positions, i)
|
||||
i=i-1
|
||||
end
|
||||
i=i+1
|
||||
j=j+1
|
||||
if i>#positions then i=1 end
|
||||
if j>k then j=1 end
|
||||
end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
local s='Survivors: '
|
||||
for _,v in pairs(positions) do s=s .. v .. ', ' end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
end
|
||||
josephus(41,3, 1)
|
||||
13
Task/Josephus-problem/REBOL/josephus-problem-1.rebol
Normal file
13
Task/Josephus-problem/REBOL/josephus-problem-1.rebol
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Rebol []
|
||||
|
||||
execute: func [death-list [block!] kill [integer!]] [
|
||||
assert [not empty? death-list]
|
||||
until [
|
||||
loop kill - 1 [append death-list take death-list]
|
||||
(1 == length? remove death-list)
|
||||
]
|
||||
]
|
||||
|
||||
prisoner: [] for n 0 40 1 [append prisoner n]
|
||||
execute prisoner 3
|
||||
print ["Prisoner" prisoner "survived"]
|
||||
3
Task/Josephus-problem/REBOL/josephus-problem-2.rebol
Normal file
3
Task/Josephus-problem/REBOL/josephus-problem-2.rebol
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
for-the-chop: [Joe Jack William Averell Rantanplan]
|
||||
execute for-the-chop 2
|
||||
print [for-the-chop "survived"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue