Random Latin squares: Difference between revisions

Content added Content deleted
No edit summary
Line 451: Line 451:


=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Python}}
<lang zkl>fcn randomLatinSquare(n,symbols=[1..]){
<lang zkl>fcn randomLatinSquare(n,symbols=[1..]){
if(n<=0) return(T);
if(n<=0) return(T);
symbols.walker().walk(n).copy() : _rls(_)
square,syms := List(), symbols.walker().walk(n);
do(n){ syms=syms.copy(); square.append(syms.append(syms.pop(0))) }
: T.zip(_.shuffle().xplode()).shuffle()
// shuffle rows, transpose & shuffle columns
T.zip(square.shuffle().xplode()).shuffle();
}
}
fcn rls2String(matrix){ matrix.apply("concat"," ").concat("\n") }</lang>
fcn _rls(symbols){ // <-->mutable list of lists
<lang zkl>foreach n in (T(1,2,5)){ randomLatinSquare(n) : rls2String(_).println("\n") }
n:=symbols.len();
randomLatinSquare(5, ["A".."Z"]) : rls2String(_).println("\n");
if(n==1) return(List(symbols)); // (1) --> ( (1) )
randomLatinSquare(10,"!@#$%^&*()") : rls2String(_).println("\n");</lang>
z,sym := (0).random(n), symbols[z];
symbols.del(z);
square:=_rls(symbols);
square.append(square[0].copy());
foreach i in (n){ square[i].insert(i,sym) }
square
}
fcn m2String(matrix){ matrix.apply("concat"," ").concat("\n") }</lang>
<lang zkl>foreach n in (T(1,2,5)){ randomLatinSquare(n) : m2String(_).println("\n") }
randomLatinSquare(5,["A".."Z"]) : m2String(_).println("\n");
randomLatinSquare(10,"!@#$%^&*()") : m2String(_).println("\n");</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 478: Line 469:
2 1
2 1


5 1 2 3 4
3 1 4 5 2
1 4 5 2 3
4 2 5 1 3
3 2 4 1 5
1 4 2 3 5
4 3 1 5 2
5 3 1 2 4
2 5 3 4 1
2 5 3 4 1


C D A E B
E D A B C
B C D A E
D C E A B
E B C D A
B A C D E
D A E B C
A E B C D
A E B C D
C B D E A


) & * # ^ @ ( % $ !
& % # ! * @ ) $ ( ^
@ ( % ! # $ * ) ^ &
@ ) * ^ # & % ( $ !
& $ # ) % ( ^ ! * @
( & % # ) $ @ ^ ! *
* # & $ @ % ! ( ) ^
! ( & % @ ^ $ * # )
% ! ( ^ $ ) & * @ #
% # ! ( ^ ) * @ & $
$ * ) & ! ^ % @ # (
^ $ @ ) & ! ( # * %
( ^ ! @ ) * # & % $
# ! ( & $ * ^ ) % @
! @ ^ % * & $ # ( )
$ @ ) * % ( & ! ^ #
^ % @ ( & # ) $ ! *
) * ^ $ ! % # & @ (
# ) $ * ( ! @ ^ & %
* ^ $ @ ( # ! % ) &
</pre>
</pre>