Created
September 26, 2016 01:42
-
-
Save rawsh/4835b0d93f48b3bfb08254025b152dbd to your computer and use it in GitHub Desktop.
USAMTS Prob. 1 Robert Washbourne Matrix Checker Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def checkHorizontal(mat): | |
for row in mat: | |
row = list(row) | |
if row.count(1) == 3 and row.count(2) == 3 and row.count(3) == 3: | |
pass | |
else: | |
return False | |
return True | |
def checkVertical(mat): | |
for i in range(mat.shape[1]): | |
col = list(mat[:, i]) | |
if col.count(1) == 3 and col.count(2) == 3 and col.count(3) == 3: | |
pass | |
else: | |
return False | |
return True | |
def checkDiagonals(mat, m): | |
n = 3 * m | |
topright = sorted(np.diag(np.fliplr(mat), n)) | |
topleft = sorted(np.diag(mat, n)) | |
bottomright = sorted(np.diag(np.fliplr(mat), -n)) | |
bottomleft = sorted(np.diag(mat, -n)) | |
return topleft == topright == bottomleft == bottomright == sorted( | |
(9 - n) / 3 * [1, 2, 3]) | |
def checkMat(mat): | |
if checkHorizontal(mat) and checkVertical(mat) and checkDiagonals( | |
mat, 0) and checkDiagonals(mat, 1) and checkDiagonals(mat, 2): | |
return True | |
else: | |
return False | |
mat = [[1, 2, 1, 3, 2, 3, 3, 1, 2], | |
[3, 3, 1, 2, 3, 2, 1, 2, 1], | |
[2, 3, 3, 2, 1, 1, 3, 2, 1], | |
[1, 2, 1, 3, 2, 1, 2, 3, 3], | |
[3, 1, 2, 1, 2, 3, 2, 1, 3], | |
[2, 1, 3, 3, 1, 1, 2, 3, 2], | |
[3, 2, 3, 2, 1, 2, 1, 1, 3], | |
[2, 1, 2, 1, 3, 3, 3, 2, 1], | |
[1, 3, 2, 1, 3, 2, 1, 3, 2]] | |
mat = np.array(mat) | |
print checkMat(mat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment