Random Latin squares: Difference between revisions

Content added Content deleted
m (use wiki markup for links)
(Added 11l)
Line 21: Line 21:
* [[oeis:A002860|OEIS: A002860]]
* [[oeis:A002860|OEIS: A002860]]
<br>
<br>

=={{header|11l}}==
{{trans|Python}}

<lang 11l>F _transpose(matrix)
assert(matrix.len == matrix[0].len)
V r = [[0] * matrix.len] * matrix.len
L(i) 0 .< matrix.len
L(j) 0 .< matrix.len
r[i][j] = matrix[j][i]
R r

F _shuffle_transpose_shuffle(matrix)
V square = copy(matrix)
random:shuffle(&square)
V trans = _transpose(square)
random:shuffle(&trans)
R trans

F _rls(&symbols)
V n = symbols.len
I n == 1
R [symbols]
E
V sym = random:choice(symbols)
symbols.remove(sym)
V square = _rls(&symbols)
square.append(copy(square[0]))
L(i) 0 .< n
square[i].insert(i, sym)
R square

F rls(n)
V symbols = Array(0 .< n)
V square = _rls(&symbols)
R _shuffle_transpose_shuffle(square)

F _check_rows(square)
V set_row0 = Set(square[0])
R all(square.map(row -> row.len == Set(row).len & Set(row) == @set_row0))

F _check(square)
V transpose = _transpose(square)
assert(_check_rows(square) & _check_rows(transpose), ‘Not a Latin square’)

L(i) [3, 3, 5, 5]
V square = rls(i)
print(square.map(row -> row.map(sym -> String(sym)).join(‘ ’)).join("\n"))
_check(square)
print()</lang>

{{out}}
<pre>
0 2 1
2 1 0
1 0 2

1 0 2
0 2 1
2 1 0

1 2 0 4 3
4 0 3 2 1
2 3 1 0 4
3 4 2 1 0
0 1 4 3 2

2 4 3 1 0
4 1 2 0 3
3 2 0 4 1
1 0 4 3 2
0 3 1 2 4
</pre>


=={{header|C}}==
=={{header|C}}==