This server is intended for use for Academic Classwork related Git repositories only. Projects/repositories will generally be removed after 6 months following close of the semester. Inactive repositories from previous semester are now being archived when no activity for 365 days. They are renamed and marked as 'archived'. After 90 days in that state they will be removed from the system completely.

Commit a06632c5 authored by august's avatar august

pa02 final commit

parent b0c4f311
'''1) Loop number of cases
2) Loop through 2d array with two while loops and find the first letter of the word by splitting the string
3) if the first letter is found in a array, then print the start pos
4) Then tell if x and y are 1 apart my subtracting x from y or y from x
Go through the word one by one until reach the end
Cycle through the word until len - 1
Append each coords to a list by adding a tuple
Take each line of letters and split by space into array
Iterate through possCoords to find what value is next to starting tuple
Then find the right starting value and iterate throught the rest of the word and 2d array to get the rest of the tuples
'''
def matrix_search(matrix, word):
wordSplit = list(word)
rows = len(matrix)
col = len(matrix[0])
startRow = 0
startCol = 0
endRow = 0
endCol = 0
currentRow = 0
currentCol = 0
coords = []
possCoords = []
for i in range(len(wordSplit)-1):
for x in range(rows):
for y in range(col):
if matrix[x][y] == wordSplit[i]:
if x + 1 < len(matrix[0]):
if matrix[x+1][y] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
if y + 1 < len(matrix[0][0]):
if matrix[x+1][y+1] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
if y - 1 >= 0:
if matrix[x+1][y-1] == wordSplit[i+1]:
startCoords = tuple([x, y])
coords.append(startCoords)
if x - 1 >= 0:
if matrix[x-1][y-1] == wordSplit[i+1]:
startCoords = tuple([x, y])
coords.append(startCoords)
if y + 1 < len(matrix[0][0]):
if matrix[x][y+1] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
if x - 1 >= 0:
if matrix[x-1][y+1] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
if y - 1 >= 0:
if matrix[x][y-1] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
if x - 1 >= 0:
if matrix[x-1][y] == wordSplit[i+1]:
startCoords = tuple([x,y])
coords.append(startCoords)
'''for i in range(1,len(wordSplit)-1):
for x in range(rows):
for y in range(col):
if wordSplit[i] == matrix[x][y]:
currentRow = x
currentCol = y
currentCoords = tuple([x,y])
coords.append(currentCoords)'''
if __name__ == '__main__':
sets = int(input())
'input()'
for i in range(sets):
rows, cols = input().split()
rows = int(rows)
cols = int(cols)
matrix = [[0 for x in range(cols)] for x in range(rows)]
for x in range(rows):
array = []
well = input()
array = well.split()
for y in range(cols):
matrix[x] = array
word = input()
'input()'
print('Searching for "' + str(word) + '" in matrix ' + str(i) + " yields:")
coords = matrix_search(matrix, word)
print('Start pos: ' + str(coords[0]) + " to End pos: " + str(coords[len(coords) - 1]) + "\n")
coords = [(0,0),(0,3),(1,0)]
for (i,j) in coords:
print (j;)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment