Created
August 1, 2018 13:30
-
-
Save samisnotinsane/a602e3a8cf949b66e2af8d255d8ca2c7 to your computer and use it in GitHub Desktop.
Manipulation and pattern recognition within a matrix. With six different examples.
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
# S. No, Student Name, Science, English, History, Arts, Maths | |
classroom = [ [1, 'Roy', 80, 75, 85, 90, 95], | |
[2, 'John', 75, 80, 75, 85, 100], | |
[3, 'Dave', 80, 80, 80, 90, 95]] | |
print('[OUT] E.g. 0') | |
print(classroom) | |
print('Pretty print: ' + '\n' + '---------') | |
for student in classroom: | |
# print(len(student)) | |
print('Name: ' + student[1] + | |
'\n' + 'Science: ' + str(student[2]) + | |
'\n') | |
print('\n' + '------' + '\n') | |
# Each element in 1st dimension is a row | |
# Each element in the 2nd dimension is a column | |
# If each row has the same no. of columns, only then it is a matrix... | |
# ...otherwise it's a nested list. | |
# E.g. 1: Initialise a 6x6 matrix with the value 0. | |
print('[OUT] E.g. 1') | |
# create 6 columns | |
eg1_matrix = [0] * 6 | |
print(eg1_matrix) | |
print('Length: ' + str(len(eg1_matrix))) | |
for row in range(6): | |
eg1_matrix[row] = [0] * 6 | |
print(eg1_matrix) | |
print('Length: ' + str(len(eg1_matrix))) | |
print('\n' + '------' + '\n') | |
# E.g. 2: Decrementing a list | |
print('[OUT] E.g. 2') | |
eg2_lst = [1,2,3,4,5] | |
eg2_lstLen = len(eg2_lst) | |
for revPtr in range(eg2_lstLen, -1, -1): | |
print(revPtr) | |
print('\n' + '------' + '\n') | |
# E.g. 3: Incrementing a list by alternate elements | |
print('[OUT] E.g. 3') | |
eg3_elements = ['a', 'b', 'c', 'd'] | |
for element in range(len(eg3_elements)): | |
if (element % 2) == 0: | |
print('Even index: ' + str(element) + ', value: ' + str(eg3_elements[element])) | |
if (element % 2) == 1: | |
print('Odd index: ' + str(element) + ', value: ' + str(eg3_elements[element])) | |
print('\n' + '------' + '\n') | |
# E.g. 4: Iterating a matrix | |
print('[OUT] E.g. 4') | |
eg4_matrix = [ | |
['a', 'b', 'c', 'd'], | |
['e', 'f', 'g', 'h'], | |
['i', 'j', 'k', 'l'] | |
] | |
width = len(eg4_matrix[0]) # x | |
height = len(eg4_matrix) # y | |
print('width: ' + str(width)) | |
print('height: ' + str(height)) | |
for y in range(height-1): | |
# print('y: ' + str(eg4_matrix[y])) | |
for x in range(width-1): | |
print(eg4_matrix[y][x]) | |
print('\n' + '------' + '\n') | |
# E.g. 5: Picking out sublists from a row | |
print('[OUT] E.g. 5') | |
eg5_matrix = [ | |
['a', 'b', 'c', 'd'], | |
['e', 'f', 'g', 'h'], | |
['i', 'j', 'k', 'l'] | |
] | |
upTo = 3 | |
width = len(eg4_matrix[0]) # x | |
height = len(eg4_matrix) # y | |
print('width: ' + str(width)) | |
print('height: ' + str(height)) | |
for y in range(height): | |
a = eg5_matrix[y] | |
for startSlice in range(len(a)): | |
endSlice = upTo-1 | |
aSublist = a[startSlice:endSlice] | |
aSet = set(aSublist) | |
if len(aSet) == 1: | |
pass | |
# Match found | |
# print('len(aSet)/match: ' + str(len(aSet))) | |
# exit() | |
else: | |
pass | |
# print('len(aSet): ' + str(len(aSet))) | |
endSlice += 1 | |
print('\n' + '------' + '\n') | |
# E.g. 6: 3-in-a-row checker | |
print('[OUT] E.g. 6') | |
eg6_matrix = [ | |
[2, 1, 1], | |
[2, 1, 1], | |
[1, 2, 2] | |
] | |
# Flag tracking if a 3-in-a-row was found in the board | |
finished = False | |
# Methods for checking 3-in-a-row | |
# Check horizontally | |
def horizontalCheck(row, col): | |
threeInARow = False | |
consecutiveCount = 0 | |
# From starting position to end of row... | |
for j in range(col, 3): | |
if eg6_matrix[row][col] == eg6_matrix[row][j]: | |
consecutiveCount += 1 | |
else: | |
break | |
if consecutiveCount >= 3: | |
threeInARow = True | |
if eg6_matrix[row][col] == 1: | |
print('p1 wins on horizontal, row: ' + str(row+1)) | |
elif eg6_matrix[row][col] == 2: | |
print('p2 wins on horizontal, row: ' + str(row+1)) | |
return threeInARow | |
# Check vertically | |
def verticalCheck(row, col): | |
threeInARow = False | |
consecutiveCount = 0 | |
for i in range(row, 3): | |
if eg6_matrix[row][col] == eg6_matrix[i][col]: | |
consecutiveCount += 1 | |
else: | |
break | |
if consecutiveCount >= 3: | |
threeInARow = True | |
if eg6_matrix[row][col] == 1: | |
print('p1 wins on vertical, col: ' + str(col+1)) | |
elif eg6_matrix[row][col] == 2: | |
print('p2 wins on vertical, col: ' + str(col+1)) | |
return threeInARow | |
# Check diagonally | |
def diagonalCheck(row, col): | |
print('row: ' + str(row) + ', col: ' + str(col)) | |
threeInARow = False | |
count = 0 | |
slope = None | |
consecutiveCount = 0 | |
j = col | |
print('scanning positive slope...') | |
for i in range(row, 3): | |
print('i: ' + str(i) + ', j: ' + str(j)) | |
if j >= 3: | |
break | |
elif eg6_matrix[row][col] == eg6_matrix[i][j]: | |
consecutiveCount += 1 | |
else: | |
break | |
# Move along 1 col, 1 row to essentially move 1 down diagonally | |
j += 1 | |
if consecutiveCount >= 3: | |
count += 1 | |
slope = 'positive' | |
if eg6_matrix[row][col] == 1: | |
print('p1 wins on diagonal, slope: ' + slope) | |
elif eg6_matrix[row][col] == 2: | |
print('p2 wins on diagonal, slope: ' + slope) | |
consecutiveCount = 0 | |
j = col | |
print('scanning negative slope...') | |
for i in range(row, -1, -1): | |
if j >= 3: | |
break | |
elif eg6_matrix[row][col] == eg6_matrix[i][j]: | |
consecutiveCount += 1 | |
else: | |
break | |
# Move along 1 col, -1 row to essentially move 1 up diagonally | |
j += 1 | |
if consecutiveCount >= 3: | |
count += 1 | |
slope = 'negative' | |
if eg6_matrix[row][col] == 1: | |
print('p1 wins on diagonal!') | |
elif eg6_matrix[row][col] == 2: | |
print('p2 wins on diagonal!') | |
if count > 0: | |
print('count (>): ' + str(count)) | |
threeInARow = True | |
if count == 2: | |
print('count (==): ' + str(count)) | |
slope = 'both' | |
return threeInARow, slope | |
# Check if there's a 3-in-a-row in the board | |
def checkForFours(): | |
# For each counter in the board... | |
for i in range(columnLength): # col elems | |
for j in range(rowLength): # row elems | |
# We only check for 3-in-a-row starting at (i,j) | |
if eg6_matrix[i][j] != 0: | |
if horizontalCheck(i, j): | |
finished = True | |
print('horizontal match!') | |
break | |
if verticalCheck(i, j): | |
finished = True | |
print('vertical match!') | |
break | |
diag_threes, slope = diagonalCheck(i, j) | |
if diag_threes: | |
print('checkForFours/slope: ' + slope) | |
finished = True | |
break | |
# Find out dimensions of board | |
columnLength = len(eg6_matrix) | |
rowLength = len(eg6_matrix[0]) | |
print('columnLength: ' + str(columnLength)) | |
print('rowLength: ' + str(rowLength)) | |
checkForFours() | |
print('\n' + '------' + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment