Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,53 @@
PROGRAM JOSEPHUS
!
! for rosettacode.org
!
!$INTEGER
DIM DEAD[100]
PROCEDURE MAIN(N,K,S->ERRORS)
! n - number of prisoners
! k - kill every k'th prisoner
! s - number of survivors
LOCAL KILLED$,SURVIVED$,FOUND,P,NN,I
ERRORS=0
FOR I=0 TO 100 DO
DEAD[I]=0
END FOR ! prepare array
PRINT("N=";N,"K=";K,"S=";S) ! show arguments
IF S>N THEN PRINT("S>N";) ERRORS+=1 END IF
IF K<=0 THEN PRINT("K<=0";) ERRORS+=1 END IF
IF ERRORS>0 THEN EXIT PROCEDURE END IF
NN=N ! wrap around boundary
P=-1 ! start here
WHILE N<>S DO ! until survivor count is met
FOUND=0 ! start looking
WHILE FOUND<>K DO ! until we have the k-th prisoner
P+=1
IF P=NN THEN P=0 END IF ! wrap around
IF DEAD[P]<>1 THEN
FOUND+=1
END IF ! if prisoner is alive increment found
END WHILE
DEAD[P]=1 ! kill the unlucky one
KILLED$=KILLED$+STR$(P) ! build killed list
N-=1 ! reduce size of circle
END WHILE
FOR I=0 TO NN-1 DO
IF DEAD[I]<>1 THEN
SURVIVED$=SURVIVED$+STR$(I) ! build survivor list
END IF
END FOR
PRINT("Killed:";KILLED$)
PRINT("Survived:";SURVIVED$)
END PROCEDURE
BEGIN
ERRORS=0
MAIN(5,2,1->ERRORS)
MAIN(41,3,1->ERRORS)
MAIN(41,3,3->ERRORS)
END PROGRAM

View file

@ -0,0 +1,13 @@
;; input
(define N 41)
(define K 3)
(define prisoners (apply circular-list (iota N)))
(define last-one prisoners) ; current position
;; kill returns current position = last killed
(define (kill lst skip)
(cond
((eq? (mark? lst) '🔫 )(kill (cdr lst) skip)) ;; dead ? goto next
((zero? skip) (mark lst '🔫)) ;; all skipped ? kill
(else (mark lst '😥 ) ;; relieved face
(kill (cdr lst ) (1- skip))))) ;; skip 1 and goto next

View file

@ -0,0 +1,22 @@
;; kill N-1
(for ((i (1- N) )) (set! last-one (kill last-one (1- K))))
;; look at prisoners
prisoners
→ ( 🔄 🔫 0 🔫 1 🔫 2 🔫 3 🔫 4 🔫 5 🔫 6 🔫 7 🔫 8 🔫 9 🔫 10 🔫 11 🔫 12 🔫 13 🔫 14 🔫 15 🔫 16
🔫 17 🔫 18 🔫 19 🔫 20 🔫 21 🔫 22 🔫 23 🔫 24 🔫 25 🔫 26 🔫 27 🔫 28 🔫 29 😥 30 🔫 31 🔫 32
🔫 33 🔫 34 🔫 35 🔫 36 🔫 37 🔫 38 🔫 39 🔫 40 🔫 0 🔫 1 … ∞)
;; #30 seems happy
;; kill last
(set! last-one (kill last-one (1- K)))
last-one
→ ( 🔫 30 🔫 31 🔫 32 …🔃 ) ;; #30 was the last
;; extra : we want more survivors
(define SURVIVORS 3)
(for ((i (- N SURVIVORS) )) (set! last-one (kill last-one (1- K))))
prisoners
→ ( 🔄 🔫 0 🔫 1 🔫 2 🔫 3 🔫 4 🔫 5 🔫 6 🔫 7 🔫 8 🔫 9 🔫 10 🔫 11 🔫 12 🔫 13 🔫 14 😥 15 🔫 16
🔫 17 🔫 18 🔫 19 🔫 20 🔫 21 🔫 22 🔫 23 🔫 24 🔫 25 🔫 26 🔫 27 🔫 28 🔫 29 😥 30 🔫 31 🔫 32
🔫 33 😥 34 🔫 35 🔫 36 🔫 37 🔫 38 🔫 39 🔫 40 🔫 0 🔫 1 🔫 0 … ∞)

View file

@ -0,0 +1,20 @@
import sequtils, strutils, future
proc j(n, k): string =
var
p = toSeq(0 .. < n)
i = 0
s = newSeq[int]()
while p.len > 0:
i = (i + k - 1) mod p.len
s.add p[i]
system.delete(p, i)
result = "Prisoner killing order: "
result.add s.map((x: int) => $x).join(", ")
result.add ".\nSurvivor: "
result.add($s[s.high])
echo j(5,2)
echo j(41,3)

View file

@ -0,0 +1,12 @@
: josephus(n, k)
| prisoners killed i |
n seq asListBuffer ->prisoners
ListBuffer newSize(n) ->killed
0 n 1- loop: i [
k 1- + prisoners size mod dup 1+ prisoners removeAt
killed add
] drop
System.Out "Killed : " << killed << "\nSurvivor : " << prisoners << cr
;

View file

@ -0,0 +1,7 @@
func josephus(n, k) {
var prisoners = @^n
while (prisoners.len > 1) {
prisoners.rotate!(k - 1).shift
}
return prisoners[0]
}

View file

@ -0,0 +1,3 @@
func josephus(n, k) {
n == 1 ? 0 : ((__FUNC__(n-1, k) + k) % n)
};

View file

@ -0,0 +1,2 @@
var survivor = josephus(41, 3);
say "Prisoner #{survivor} survived.";

View file

@ -0,0 +1,45 @@
class Josephus {
class func lineUp(#numberOfPeople:Int) -> [Int] {
var people = [Int]()
for (var i = 0; i < numberOfPeople; i++) {
people.append(i)
}
return people
}
class func execute(#numberOfPeople:Int, spacing:Int) -> Int {
var killIndex = 0
var people = self.lineUp(numberOfPeople: numberOfPeople)
println("Prisoners executed in order:")
while (people.count > 1) {
killIndex = (killIndex + spacing - 1) % people.count
executeAndRemove(&people, killIndex: killIndex)
}
println()
return people[0]
}
class func executeAndRemove(inout people:[Int], killIndex:Int) {
print("\(people[killIndex]) ")
people.removeAtIndex(killIndex)
}
class func execucteAllButM(#numberOfPeople:Int, spacing:Int, save:Int) -> [Int] {
var killIndex = 0
var people = self.lineUp(numberOfPeople: numberOfPeople)
println("Prisoners executed in order:")
while (people.count > save) {
killIndex = (killIndex + spacing - 1) % people.count
executeAndRemove(&people, killIndex: killIndex)
}
println()
return people
}
}
println("Josephus is number: \(Josephus.execute(numberOfPeople: 41, spacing: 3))")
println()
println("Survivors: \(Josephus.execucteAllButM(numberOfPeople: 41, spacing: 3, save: 3))")

View file

@ -0,0 +1,12 @@
# A control structure, for convenience:
# as soon as "condition" is true, then emit . and stop:
def do_until(condition; next):
def u: if condition then . else (next|u) end;
u;
# n is the initial number; every k-th prisoner is removed until m remain.
# Solution by simulation
def josephus(n;k;m):
reduce range(0;n) as $i ([]; . + [$i]) # Number the prisoners from 0 to (n-1)
| do_until( length < k or length <= m; .[k:] + .[0:k-1] )
| do_until( length <= m; (k % length) as $i | .[$i:] + .[0:$i-1] );

View file

@ -0,0 +1,5 @@
def task(n;k;m):
"Survivors for n=\(n), k=\(k), m=\(m): \( josephus(n;k;m) )";
task(41;3;1),
task(23482; 3343; 3)