Latin square problem - python achieve

Problem Description

Is a Latin square matrix of n × n matrix, the matrix has exactly n different elements, each element exactly with n, and each of the elements appear in a row and just one column. The famous physicist and mathematician Euler using the Latin alphabet as symbols of elements in a Latin square Latin square hence the name.
For example:
1 2 3
2 3 1
3 1 2
problems: how to construct N-order Latin square?

List

def solution_list(n):
    for y in range(n):
        for x in range(n):
            num = (y + x) % n + 1
            print(f'{num} ', end='')
        print('\n')

Round robin list

class Node:
    """节点"""

    def __init__(self, value):
        self.data = value
        self.next = None

    def __repr__(self):
        return f'Node: {self.data}'


class CircularLinkedList:
    """单循环链表"""
    def __init__(self):
        self.rear = None    # 尾节点

    def append(self, elem):
        """尾插法"""
        temp = Node(elem)
        if self.rear is None:
            temp.next = temp
            self.rear = temp
        else:
            temp.next = self.rear.next
            self.rear.next = temp
            self.rear = temp


def solution_circular_linked_list(n):
    clist = CircularLinkedList()
    for i in range(1, n + 1):
        clist.append(i)

    cur = clist.rear
    while cur.next is not clist.rear:
        cur = cur.next
        temp = cur
        for _ in range(n):
            print(f'{temp.data} ', end='')
            temp = temp.next
        print('\n')

Guess you like

Origin www.cnblogs.com/thunderLL/p/12072068.html