Jump to content

Random Latin squares: Difference between revisions

m (→‎{{header|Python}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 450:
 
=={{header|zkl}}==
{{trans|Python}}
<lang zkl></lang>
<lang zkl></lang>fcn randomLatinSquare(n,symbols=[1..]){
if(n<=0) return(T);
symbols.walker().walk(n).copy() : _rls(_)
: T.zip(_.shuffle().xplode()).shuffle()
}
fcn _rls(symbols){ // <-->mutable list of lists
n:=symbols.len();
if(n==1) return(List(symbols)); // (1) --> ( (1) )
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}}
<pre>
1
 
1 2
2 1
 
5 1 2 3 4
1 4 5 2 3
3 2 4 1 5
4 3 1 5 2
2 5 3 4 1
 
C D A E B
B C D A E
E B C D A
D A E B C
A E B C D
 
) & * # ^ @ ( % $ !
@ ( % ! # $ * ) ^ &
& $ # ) % ( ^ ! * @
* # & $ @ % ! ( ) ^
% ! ( ^ $ ) & * @ #
$ * ) & ! ^ % @ # (
( ^ ! @ ) * # & % $
! @ ^ % * & $ # ( )
^ % @ ( & # ) $ ! *
# ) $ * ( ! @ ^ & %
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.