Generating a Latin Square (Modified Algorithm) [closed]

Posted on


Sifting is used to create a Latin Square of distinct symbols. The sifting is implemented by swaps on an array. The symbols might be strings or integer, but they are required to be distinct. Notably, this requires deep copy so that the whole Latin Square is not sifted by the same 1.

import copy 

def make_latin_square(symbols):
   if symbols is None:
       raise ValueError("symbols is required to not be None")
   square = [] 
   for row in range(len(symbols)):
       symbols[0], symbols[row] = symbols[row], symbols[0]
   return square 
def test_make_latin_square():
   assert make_latin_square([0]) == [[0]]
   assert make_latin_square([0, 1, 2]) == [[0, 1, 2], [1, 0, 2], [2, 0, 1]]
   assert make_latin_square([-1, -2, -3]) == [[-1, -2, -3], [-2, -1, -3], [-3, -1, -2]]



The bug is that a swap between two elements will not result in a Latin square. Instead, (row + col) % (rows * cols) generates a unique number for each element of the Latin square.

Leave a Reply

Your email address will not be published. Required fields are marked *