How to resolve the algorithm Random Latin squares step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Random Latin squares step by step in the 11l programming language

Table of Contents

Problem Statement

A Latin square of size n is an arrangement of n symbols in an n-by-n square in such a way that each row and column has each symbol appearing exactly once. For the purposes of this task, a random Latin square of size n is a Latin square constructed or generated by a probabilistic procedure such that the probability of any particular Latin square of size n being produced is non-zero. Strict uniformity in the random generation is a hard problem and not a requirement of the task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Random Latin squares step by step in the 11l programming language

Source code in the 11l programming language

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.join(‘ ’)).join("\n"))
   _check(square)
   print()

  

You may also check:How to resolve the algorithm Permutations step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the BQN programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the Scala programming language