0

If I have a grid of 3x3, and it's for a latin square board

Where each row is a unique set of characters.

My question is; I have currently checked if the grid is a n x n grid, but how can I check if there is a "n" different amount of characters used i.e. 3 characters such as ABC

Am I supposed to use a count function? Or am i supposed to integrate this question "code" into my square function:

Maybe if I clarify my question; I want to check if theres an N amount of characters. Such as if the nxn function checks if its 3x3, there should also be 3 Different characters, it doesn't need to check if they are ABC in different orders but just any 3 different characters used

Xrin
  • 87
  • 1
  • 3
  • 15
  • Possible duplicate of [Using dynamic multi-dimensional arrays in c++](http://stackoverflow.com/questions/1471721/using-dynamic-multi-dimensional-arrays-in-c) – Prune Mar 04 '16 at 18:21
  • 1
    Each row is not a unique set of characters. The set `{A,B,C}` is the same as `{B,C,A}` and `{C,A,B}`. Do you mean that each row is unique? – AMACB Mar 04 '16 at 18:21
  • 2
    @Prune The OP is using Python, not C++ – AMACB Mar 04 '16 at 18:21
  • I believe that the algorithm should suffice? – Prune Mar 04 '16 at 18:22
  • 3
    `for row in rows: if len(set(row)) != len(row): #not unique` – zondo Mar 04 '16 at 18:23
  • [This question](http://stackoverflow.com/questions/35761604/python-latin-square-column-and-row-validation) is farther along in the solution path. – Prune Mar 04 '16 at 18:24
  • You can use a Counter (package collections) to see that each of N characters is used exactly once; you can check each character against the rest of the string. You can use a set of the characters and see whether it's length is the row length (now a previous comment). You'll want to generate columns using a comprehension, for whatever type you use to evaluate the rows (set, counter, etc.). – Prune Mar 04 '16 at 18:27
  • Maybe if I clarify my question; I want to check if theres an N amount of characters. Such as if the nxn function checks if its 3x3, there should also be 3 Different characters, it doesn't need to check if they are ABC in different orders but just any 3 different characters used – Xrin Mar 04 '16 at 18:31

1 Answers1

1

To see in a line only contains different characters, just make a set with it: if there are duplicates, the length of the set will be shorter than the original line. You function could become:

def square(sq):
    rows = len(sq)
    for row in sq:
        if (len(row) != rows) or (len(set(row)) != rows):
            return False
    return True
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    Credits for this should remain to @zondo who first gave the solution in its comment – Serge Ballesta Mar 04 '16 at 19:01
  • With this function @Serge Ballesta ;it does check if "n" amounts of characters are used, but how do i make it check if it is EXACTLY n amount of characters and not just >= n? – Xrin Mar 05 '16 at 19:15