-1

My current function checks if there is at least a certain amount of characters i.e. If I have a 3x3 square grid, it will check if there is 3 characters or more but I want to modify the code to check if its EXACTLY 3 characters not more.

def double(char):
    rows = len(char)
    for row in char:
        if (len(set(row)) != rows):
            return False  
    return True

It's different to when i check for a square grid:

def square(sq):
    rows = len(sq)
    for row in sq:
        if (len(row) != rows):
            return False  
    return True

In my main code; i raise the exception and call the error:

 if not double(d):
                raise ValueError
            break

        except ValueError:
            if not square(d):
                print("The format is incorrect; Has to be in a n x n square format")
            elif not double(d):
                print("The grid does not contain exactly n amount of characters")
Xrin
  • 87
  • 1
  • 3
  • 15
  • What is `char`? And why do you call the function `double`? –  Mar 06 '16 at 16:38
  • 1
    Possible duplicate of [Python - n Different characters used in a grid](http://stackoverflow.com/questions/35803294/python-n-different-characters-used-in-a-grid) –  Mar 06 '16 at 16:40
  • That was initially my question anyway however this is different because its asking for an EXACT check; ill update the main post to include where the function is being called – Xrin Mar 06 '16 at 16:43

2 Answers2

1

The names of your variables are confusing, for example the variable char represents a square_grid, the function called double checks for a 3x3 grid.

sg1 = ['ABC','DEF','GHI']
sg2 = ['WXYZ','AEIOUY']
sg3 = ['ABV','CAB','BCA']
sg4 = ['ABC','CAB','BCA']


def verify3x3( square_grid ):
  nb_rows = 0
  for row in square_grid:
    if len(row) != 3:
      return False
    nb_rows += 1
  return (nb_rows == 3)   

print verify3x3(sg1)
print verify3x3(sg2)
print verify3x3(sg3)
print verify3x3(sg4)

With python2, this prints

True
False
True
True

EDIT: If you want to count distinct characters, and verify how many there are:

def verify_3distinct_chars( square_grid ):
  chars_list = []
  for row in square_grid:
    for c in row:
      if c not in chars_list:
        chars_list.append(c)
  return len(chars_list) == 3

print verify_3distinct_chars( sg1 )   
print verify_3distinct_chars( sg2 )   
print verify_3distinct_chars( sg3 )   
print verify_3distinct_chars( sg4 )   

With python2, this prints

False
False
False
True

N.b. you could write this much shorter, but I detailed the logic steps.

Sci Prog
  • 2,651
  • 1
  • 10
  • 18
  • Yes, this is fine. My current code checks if it's a 3x3 or not but the problem is if i have a latin square board [ABV,CAB,BCA] this has 4 characters, abc and v, if this is the case an error should appear by using the function. But at the moment it still prints the whole grid. – Xrin Mar 06 '16 at 16:56
  • Ok, so you want to count how many distinct characters are in your array, then verify if there are 3. You can use a `list` to gather the distinct characters. I edited my answer. – Sci Prog Mar 06 '16 at 17:22
  • Precisely, apart from one factor. A latin square board can vary in size, it may be a 4x4, 5x5 etc. Im experimenting with a 3x3 but i guess i can just find the length of the row and replace the 3 with the row length? – Xrin Mar 06 '16 at 17:25
  • You should have no difficulty of generalizing to more than 3. – Sci Prog Mar 06 '16 at 17:31
  • Well no, I mean obviously i could change 3 to 4 etc But i mean; since this is reading from a text file it should be automatically be able to tell if its a 3x3 or a 4x4; if that makes sense. – Xrin Mar 06 '16 at 17:36
  • 1
    To make your code easy to understand, maintain and reuse, each function should have a single well defined task. Therefore, figuring the size of the square and checking how many distinct characters are present should be two separate functions. – Sci Prog Mar 06 '16 at 17:45
0

Hope this helps:

def double(char):
    for row in char:
        if len(char) != 3 or len(row) != 3:
            return False
        else:
            return True
Cosinux
  • 321
  • 1
  • 4
  • 16
  • Hmm not quite; if i have 4 characters i.e. ABC and V it still loads the code and shows the square grid. And for your information im creating a latin square board. – Xrin Mar 06 '16 at 16:49
  • I edited it now, but I don't quite understand you. You want to check if all the rows and lines are 3 characters long or something else? – Cosinux Mar 06 '16 at 16:56
  • If i have a latin square board with a square grid [ABV,CBA,BCA] it has 4 characters right and is a 3x3 grid. ABC and V. However the check needs to make sure the whole grid contains ONLY 3 characters if theres more or less an error will appear. This shouldn't apply to only 3x3 but a n x n grid. Does that make any more sense? – Xrin Mar 06 '16 at 17:01