Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import 'sys' library in order to read from the terminal
- import sys
- # Class Matrix (object for holding a matrix)
- class Matrix:
- # Class variables
- theMatrix = []
- width = 0
- height = 0
- def __init__(self, line, is_test):
- # Clears the matrix every time, a new one is made
- self.theMatrix.clear()
- # If it is true this means that the given string is name of a test file
- if is_test:
- # Reads from the file
- file = open("tests\\" + line)
- # First line is the file contains the parameters of the matrix
- new_line = file.readline()
- (given_height, sep, given_width) = new_line.strip().partition(' ')
- # Sets the class variable
- self.width = int(given_width)
- self.height = int(given_height)
- # Reads the matrix from the file
- for line in file:
- row = line.split()
- self.theMatrix.append(row)
- file.close()
- # If it is false - the line contains the parameters of the matrix
- else:
- (given_height, sep, given_width) = line.strip().partition(' ')
- # Sets the class variable
- self.height = int(given_height)
- self.width = int(given_width)
- # Reads the matrix from the terminal
- for i in range(self.height):
- new_line = input(str(i) + ": ").strip().split()
- if len(new_line) != self.width:
- print("The number of colors on each line should be equal"
- + "to the given number of columns!")
- exit()
- self.theMatrix.append(new_line)
- # Function for printing the matrix (it is not used in the main method)
- def print_matrix(self):
- for i in range(len(self.theMatrix)):
- print(str(i) + ': ', end='')
- for j in range(len(self.theMatrix[i])):
- print(self.theMatrix[i][j], end=' ')
- print()
- # Function for the getting the area of the matrix
- def area(self):
- return self.height * self.width
- # Function for finding the longest path from given cell(i, j)
- def sequence(self, i, j, sequence_length, present):
- # Saving the coordinates of the starting cell in different variables
- start_row = i
- start_col = j
- # Sets the sequence_length of cell (start_row, start_col) to 0
- sequence_length[start_row][start_col] = 0
- # While the cell (i, j) is in the matrix, execute:
- while not (i < 0 or i >= self.height or j < 0 or j >= self.width):
- # Sets the cell (i, j) as true in order to know later if this cell
- # is been in the while or not
- present[i][j] = True
- # Checks if the cell (i, j) has already had longest path
- if sequence_length[i][j] != -1:
- # If the path till now is less than the path from cell(i, j)
- if sequence_length[start_row][start_col] < sequence_length[i][j]:
- # Set the path with biggest number and exit the while
- sequence_length[start_row][start_col] = sequence_length[i][j]
- break
- # Increases the path with one
- sequence_length[start_row][start_col] += 1
- # Sets the path of current cell to be equal the one of starting cell
- sequence_length[i][j] = sequence_length[start_row][start_col]
- # Checks if the RIGHT cell has the same element as current
- if (j < self.width - 1
- and ((self.theMatrix[i][j]) == self.theMatrix[i][j + 1])
- and not (present[i][j + 1])):
- # Goes RIGHT
- j += 1
- continue
- # Checks if the LEFT cell has the same element as current
- if (j > 0
- and (self.theMatrix[i][j] == self.theMatrix[i][j - 1])
- and not (present[i][j - 1])):
- # Goes LEFT
- j -= 1
- continue
- # Checks if the DOWN cell has the same element as current
- if (i > 0
- and (self.theMatrix[i][j] == self.theMatrix[i - 1][j])
- and not (present[i - 1][j])):
- # Goes DOWN
- i -= 1
- continue
- # Checks if the UP cell has the same element as current
- if (i < self.height - 1
- and (self.theMatrix[i][j] == self.theMatrix[i + 1][j])
- and not (present[i + 1][j])):
- # Goes UP
- i += 1
- continue
- # Exit if there other direction to go
- break
- # Return the path length of the given cell
- return sequence_length[start_row][start_col]
- # Function for finding the longest path in the matrix
- def longest_sequence(self):
- # Setting the longest sequence to be 1
- longest_seq = 1
- # Matrix for holding the length of every sequence_length
- sequence_length = [[-1 for i in range(self.width)]
- for i in range(self.height)]
- # Goes through every cell in the matrix
- for i in range(self.height):
- for j in range(self.width):
- if sequence_length[i][j] == -1:
- # Boolean matrix whether the cell has been part of the path
- present = [[False for i in range(self.width)]
- for i in range(self.height)]
- # Finding the longest path from every cell
- sequence_length[i][j] = self.sequence(i, j, sequence_length, present)
- # Finding the longestSequence, so far
- longest_seq = max(longest_seq, sequence_length[i][j])
- # If the longest path is equal to the area that this means
- # there cannot be found longer path than this
- if longest_seq == self.area():
- break
- # Returns the result
- return longest_seq
- # Creating a boolean variables
- is_test = None
- # Checks if there are additional parameters
- if 1 < len(sys.argv) <= 5:
- # Sets the variable to True
- is_test = True
- for numOfParm in range(1, len(sys.argv)):
- # For every given parameter, it creates a matrix
- matrix = Matrix(sys.argv[numOfParm], is_test)
- # Print the result after calling the function for finding longest path
- print("The number of the longest adjacent sequence from "
- + sys.argv[numOfParm] + " is " + str(matrix.longest_sequence()))
- # This means there are no additional parameters
- elif len(sys.argv) == 1:
- # Sets the variable to True
- is_test = False
- # Getting the parameters of the wanted matrix
- first_line = input("Please, enter the size of the matrix (rows columns): ")
- # Creates a matrix with given parameters
- matrix = Matrix(first_line, is_test)
- # Print the result after calling the function for finding the longest path
- print("The number of the longest adjacent sequence is "
- + str(matrix.longest_sequence()))
- # There are more than 4 additional parameters
- else:
- print("The program accepts from one to four additional parameters!")
Advertisement
Add Comment
Please, Sign In to add comment