Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,12 @@
BEGIN
PROC josephus = (INT n, k, m) INT :
CO Return m-th on the reversed kill list; m=0 is final survivor. CO
BEGIN
INT lm := m; CO Local copy of m CO
FOR a FROM m+1 WHILE a <= n DO lm := (lm+k) %* a OD;
lm
END;
INT n = 41, k=3;
printf (($"n = ", g(0), ", k = ", g(0), ", final survivor: ", g(0)l$,
n, k, josephus (n, k, 0)))
END

View file

@ -0,0 +1,32 @@
@echo off
setlocal enabledelayedexpansion
set "prison=41" %== Number of prisoners ==%
set "step=3" %== The step... ==%
set "survive=1" %== Number of survivors ==%
call :josephus
set "prison=41"
set "step=3"
set "survive=3"
call :josephus
pause
exit /b 0
%== The Procedure ==%
:josephus
set "surv_list="
for /l %%S in (!survive!,-1,1) do (
set /a "m = %%S - 1"
for /l %%X in (%%S,1,!prison!) do (
set /a "m = (m + step) %% %%X"
)
if defined surv_list (
set "surv_list=!surv_list! !m!"
) else (
set "surv_list=!m!"
)
)
echo !surv_list!
goto :EOF

View file

@ -0,0 +1,4 @@
>0" :srenosirP">:#,_&>>00p>>v
v0p01<&_,#!>#:<"Step size: "<
>1+:20p00g`!#v_0" :rovivru"v
^g02%g02+g01<<@.$_,#!>#:<"S"<

View file

@ -0,0 +1,13 @@
(defn rotate [n s] (lazy-cat (drop n s) (take n s)))
(defn josephus [n k]
(letfn [(survivor [[ h & r :as l] k]
(cond (empty? r) h
:else (survivor (rest (rotate (dec k) l)) k)))]
(survivor (range n) k)))
(let [n 41 k 3]
(println (str "Given " n " prisoners in a circle numbered 1.." n
", an executioner moving around the"))
(println (str "circle " k " at a time will leave prisoner number "
(inc (josephus n k)) " as the last survivor.")))

View file

@ -0,0 +1,17 @@
import std.stdio, std.algorithm, std.range;
int[][] Josephus(in int n, int k, int s=1) {
int[] ks, ps = n.iota.array;
for (int i=--k; ps.length>s; i=(i+k)%ps.length) {
ks ~= ps[i];
ps = remove(ps, i);
}
writefln("Josephus(%d,%d,%d) -> %(%d %) / %(%d %)%s", n, k, s, ps, ks[0..min($,45)], ks.length<45 ? "" : " ..." );
return [ps, ks];
}
void main() {
Josephus(5, 2);
Josephus(41, 3);
Josephus(23482, 3343, 3);
}}

View file

@ -0,0 +1,52 @@
class
APPLICATION
create
make
feature
make
do
io.put_string ("Survivor is prisoner: " + execute (12, 4).out)
end
execute (n, k: INTEGER): INTEGER
-- Survivor of 'n' prisoners, when every 'k'th is executed.
require
n_positive: n > 0
k_positive: k > 0
n_larger: n > k
local
killidx: INTEGER
prisoners: LINKED_LIST [INTEGER]
do
create prisoners.make
across
0 |..| (n - 1) as c
loop
prisoners.extend (c.item)
end
io.put_string ("Prisoners are executed in the order:%N")
killidx := 1
from
until
prisoners.count <= 1
loop
killidx := killidx + k - 1
from
until
killidx <= prisoners.count
loop
killidx := killidx - prisoners.count
end
io.put_string (prisoners.at (killidx).out + "%N")
prisoners.go_i_th (killidx)
prisoners.remove
end
Result := prisoners.at (1)
ensure
Result_in_range: Result >= 0 and Result < n
end
end

View file

@ -0,0 +1,15 @@
defmodule Josephus do
def find(n,k) do
find(Enum.to_list(0..n-1),0..k-2,k..n)
end
def find([_|[r|_]],_,_..d) when d < 3 do
IO.inspect r
end
def find(arr,a..c,b..d) when length(arr) >= 3 do
find(Enum.slice(arr,b..d) ++ Enum.slice(arr,a..c),a..c,b..d-1)
end
end
Josephus.find(41,3)

View file

@ -1,4 +1,4 @@
Josephus2 =: 4 : '(|x&+)/i.->:y' NB. this is a direct translation of the algo from C code above.
Josephus2 =: 4 : '(| x&+)/i. - 1+y' NB. this is a direct translation of the algo from C code above.
3 Josephus2 41
30

View file

@ -0,0 +1,37 @@
import java.util.ArrayList;
import java.util.List;
public class Josephus {
public static void main(String[] args) {
execute(5, 1);
execute(41, 2);
execute(23482, 3342, 3);
}
public static int[][] execute(int n, int k) {
return execute(n, k, 1);
}
public static int[][] execute(int n, int k, int s) {
List<Integer> ps = new ArrayList<Integer>(n);
for (int i=0; i<n; i+=1) ps.add(i);
List<Integer> ks = new ArrayList<Integer>(n-s);
for (int i=k; ps.size()>s; i=(i+k)%ps.size()) ks.add(ps.remove(i));
System.out.printf("Josephus(%d,%d,%d) -> %s / %s\n", n, k, s, toString(ps), toString(ks));
return new int[][] {
ps.stream().mapToInt(Integer::intValue).toArray(),
ks.stream().mapToInt(Integer::intValue).toArray()
};
}
private static String toString(List <Integer> ls) {
String dot = "";
if (ls.size() >= 45) {
dot = ", ...";
ls = ls.subList(0, 45);
}
String s = ls.toString();
return s.substring(1, s.length()-1) + dot;
}
}

View file

@ -0,0 +1,7 @@
function Josephus(n, k, s) {
s = s | 1
for (var ps=[], i=n; i--; ) ps[i]=i
for (var ks=[], i=--k; ps.length>s; i=(i+k)%ps.length) ks.push(ps.splice(i, 1))
document.write((arguments.callee+'').split(/\s|\(/)[1], '(', [].slice.call(arguments, 0), ') -> ', ps, ' / ', ks.length<45?ks:ks.slice(0,45)+',...' , '<br>')
return [ps, ks]
}

View file

@ -0,0 +1,20 @@
<?php //Josephus.php
function Jotapata($n=41,$k=3,$m=1){$m--;
$prisoners=array_fill(0,$n,false);//make a circle of n prisoners, store false ie: dead=false
$deadpool=1;//count to next execution
$order=0;//death order and *dead* flag, ie. deadpool
while((array_sum(array_count_values($prisoners))<$n)){//while sum of count of unique values dead times < n (they start as all false)
foreach($prisoners as $thisPrisoner=>$dead){
if(!$dead){//so yeah...if not dead...
if($deadpool==$k){//if their time is up in the deadpool...
$order++;
//set the deadpool value or enumerate as survivor
$prisoners[$thisPrisoner]=((($n-$m)>($order)?$order:(($n)==$order?'Call me *Titus Flavius* Josephus':'Joe\'s friend '.(($order)-($n-$m-1)))));
$deadpool=1;//reset count to next execution
}else{$duckpool++;}
}
}
}
return $prisoners;
}
echo '<pre>'.print_r(Jotapata(41,3,5),true).'<pre>';

View file

@ -1,4 +1,4 @@
sub Execute(@prisoner is rw, $k) {
sub Execute(@prisoner, $k) {
until @prisoner == 1 {
@prisoner.=rotate($k - 1);
@prisoner.shift;

View file

@ -0,0 +1,41 @@
function main($n=0,$k=0,$s=0) {
#n - number of prisoners
#k - kill every k'th prisoner
#s - number of survivors
write-host "`nn=$n k=$k s=$s" #show arguments
#Error Checking (Optional)
try {
if ([int]$n -lt 0){write-host "[n`<0] " -nonewline;$errors++}
if ([int]$k -lt 0){write-host "[k`<0] " -nonewline;$errors++}
if ([int]$s -lt 0){write-host "[s`<0] " -nonewline;$errors++}
if ([int]$s -gt [int]$n){write-host "[s`>n] " -nonewline;$errors++}
if ($errors -gt 0) {"";return}
} catch {"Oops! I found a string input.";return}
$dead = @(0) * $n+1
$nn=$n
$p=-1
while ($n -ne $s){
$found=0
while ($found -ne $k){
if ($p++ -eq $nn) { $p=0 }
if ($dead[$p] -ne 1) {$found++}
}
$dead[$p]++
$killed+="$p "
$n--
}
for($i=0;$i -le $nn-1;$i++){
if ($dead[$i] -ne 1) {$survived+="$i "}
}
write-host "Killed: $killed"
write-host "Survived: $survived"
return
}
main 5 2 1
main 41 3 1
main 41 3 3
main 2 -3 4

View file

@ -0,0 +1,35 @@
Define.i
NewList prisoners.i()
Procedure f2l(List p.i())
FirstElement(p()) : tmp.i=p()
DeleteElement(p(),1) : LastElement(p())
AddElement(p()) : p()=tmp
EndProcedure
Procedure l2f(List p.i())
LastElement(p()) : tmp.i=p()
DeleteElement(p()) : FirstElement(p())
InsertElement(p()) : p()=tmp
EndProcedure
OpenConsole()
Repeat
Print(#LF$+#LF$)
Print("Josephus problem - input prisoners : ") : n=Val(Input())
If n=0 : Break : EndIf
Print(" - input steps : ") : k=Val(Input())
Print(" - input survivors : ") : s=Val(Input()) : If s<1 : s=1 : EndIf
ClearList(prisoners()) : For i=0 To n-1 : AddElement(prisoners()) : prisoners()=i : Next
If n<100 : Print("Executed : ") : EndIf
While ListSize(prisoners())>s And n>0 And k>0 And k<n
For j=1 To k : f2l(prisoners()) : Next
l2f(prisoners()) : FirstElement(prisoners()) : If n<100 : Print(Str(prisoners())+Space(2)) : EndIf
DeleteElement(prisoners())
Wend
Print(#LF$+"Surviving: ")
ForEach prisoners()
Print(Str(prisoners())+Space(2))
Next
ForEver
End

View file

@ -1,25 +1,25 @@
/*REXX pgm, Josephus problem: N men standing in a circle, every Kth kilt*/
parse arg N K Z R . /*get optional arguments. */
if N==',' | N=='' then N = 41 /*no #prisoners? Then use default*/
if K==',' | K=='' then K = 3 /*no kill count? " " " */
if Z==',' | Z=='' then Z = 0 /*no initial # ? " " " */
if R==',' | R=='' then R = 1 /*no remaining#? " " " */
$=; x=; do pop=Z for N; $=$ pop; end /*populate the prisoner's circle.*/
c=0 /*initial prisoner count-off num.*/
do remove=0; p=words($) /*keep removing until R are left.*/
c=c+K /*bump prisoner count-off by K.*/
if c>p then do /* [↓] remove some prisoner(s).*/
/*REXX program: Josephus problem: N men standing in a circle, every Kth kilt.*/
parse arg N K Z R . /*get the optional arguments from C.L. */
if N==',' | N=='' then N = 41 /*no #prisoners? Then use the default*/
if K==',' | K=='' then K = 3 /*no kill count? " " " " */
if Z==',' | Z=='' then Z = 0 /*no initial # ? " " " " */
if R==',' | R=='' then R = 1 /*no remaining#? " " " " */
$=; x=; do pop=Z for N; $=$ pop; end /*populate prisoner's circle (with a #)*/
c=0 /*initial prisoner count─off number. */
do remove=0; p=words($) /*keep removing until R are remaining*/
c=c+K /*bump the prisoner count-off by K. */
if c>p then do /* [↓] remove (kill) some prisoner(s)*/
do j=1 for words(x); $=delword($,word(x,j)+1-j,1)
if words($)==R then leave remove /*slaying done yet?*/
if words($)==R then leave remove /*is the slaying done? */
end /*j*/
c=(c//p)//words($); x= /*adjust prisoner count-off &list*/
c=(c//p)//words($); x= /*adjust prisoner count-off and circle.*/
end
if c\==0 then x=x c /*list of prisoners to be removed*/
end /*remove*/ /*remove 'til R prisoners left.*/
if c\==0 then x=x c /*the list of prisoners to be removed. */
end /*remove*/ /*remove 'til R prisoners are left.*/
say 'removing every ' th(K) " prisoner out of " N ' (starting at' Z") with ",
R ' survivor's(R)"," ; say 'leaving prisoner's(R)':' $
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines──────────────────────────*/
say 'removing every ' th(K) " prisoner out of " N ' (starting at' Z") with ",
R ' survivor's(R)","; say 'leaving prisoner's(R)':' $
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────subroutines───────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
th: arg y; return y||word('th st nd rd', 1+y//10*(y//100%10\==1)*(y//10<4))
th: parse arg y; return y||word('th st nd rd',1+y//10*(y//100%10\==1)*(y//10<4))

View file

@ -0,0 +1,32 @@
Function josephus(n,k,s)
Set prisoner = CreateObject("System.Collections.ArrayList")
For i = 0 To n - 1
prisoner.Add(i)
Next
index = -1
Do Until prisoner.Count = s
step_count = 0
Do Until step_count = k
If index+1 <= prisoner.Count-1 Then
index = index+1
Else
index = (index+1)-(prisoner.Count)
End If
step_count = step_count+1
Loop
prisoner.RemoveAt(index)
index = index-1
Loop
For j = 0 To prisoner.Count-1
If j < prisoner.Count-1 Then
josephus = josephus & prisoner(j) & ","
Else
josephus = josephus & prisoner(j)
End If
Next
End Function
'testing the function
WScript.StdOut.WriteLine josephus(5,2,1)
WScript.StdOut.WriteLine josephus(41,3,1)
WScript.StdOut.WriteLine josephus(41,3,3)