yordan_yordanov

my_solution

Oct 26th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. # Import 'sys' library in order to read from the terminal
  2. import sys
  3.  
  4.  
  5. # Class Matrix (object for holding a matrix)
  6. class Matrix:
  7. # Class variables
  8. theMatrix = []
  9. width = 0
  10. height = 0
  11.  
  12. def __init__(self, line, is_test):
  13.  
  14. # Clears the matrix every time, a new one is made
  15. self.theMatrix.clear()
  16.  
  17. # If it is true this means that the given string is name of a test file
  18. if is_test:
  19. # Reads from the file
  20. file = open("tests\\" + line)
  21. # First line is the file contains the parameters of the matrix
  22. new_line = file.readline()
  23.  
  24. (given_height, sep, given_width) = new_line.strip().partition(' ')
  25.  
  26. # Sets the class variable
  27. self.width = int(given_width)
  28. self.height = int(given_height)
  29.  
  30. # Reads the matrix from the file
  31. for line in file:
  32. row = line.split()
  33. self.theMatrix.append(row)
  34.  
  35. file.close()
  36. # If it is false - the line contains the parameters of the matrix
  37. else:
  38. (given_height, sep, given_width) = line.strip().partition(' ')
  39.  
  40. # Sets the class variable
  41. self.height = int(given_height)
  42. self.width = int(given_width)
  43.  
  44. # Reads the matrix from the terminal
  45. for i in range(self.height):
  46. new_line = input(str(i) + ": ").strip().split()
  47. if len(new_line) != self.width:
  48. print("The number of colors on each line should be equal"
  49. + "to the given number of columns!")
  50. exit()
  51. self.theMatrix.append(new_line)
  52.  
  53. # Function for printing the matrix (it is not used in the main method)
  54. def print_matrix(self):
  55. for i in range(len(self.theMatrix)):
  56. print(str(i) + ': ', end='')
  57. for j in range(len(self.theMatrix[i])):
  58. print(self.theMatrix[i][j], end=' ')
  59. print()
  60.  
  61. # Function for the getting the area of the matrix
  62. def area(self):
  63. return self.height * self.width
  64.  
  65. # Function for finding the longest path from given cell(i, j)
  66. def sequence(self, i, j, sequence_length, present):
  67.  
  68. # Saving the coordinates of the starting cell in different variables
  69. start_row = i
  70. start_col = j
  71. # Sets the sequence_length of cell (start_row, start_col) to 0
  72. sequence_length[start_row][start_col] = 0
  73.  
  74. # While the cell (i, j) is in the matrix, execute:
  75. while not (i < 0 or i >= self.height or j < 0 or j >= self.width):
  76. # Sets the cell (i, j) as true in order to know later if this cell
  77. # is been in the while or not
  78. present[i][j] = True
  79.  
  80. # Checks if the cell (i, j) has already had longest path
  81. if sequence_length[i][j] != -1:
  82. # If the path till now is less than the path from cell(i, j)
  83. if sequence_length[start_row][start_col] < sequence_length[i][j]:
  84. # Set the path with biggest number and exit the while
  85. sequence_length[start_row][start_col] = sequence_length[i][j]
  86. break
  87.  
  88. # Increases the path with one
  89. sequence_length[start_row][start_col] += 1
  90. # Sets the path of current cell to be equal the one of starting cell
  91. sequence_length[i][j] = sequence_length[start_row][start_col]
  92.  
  93. # Checks if the RIGHT cell has the same element as current
  94. if (j < self.width - 1
  95. and ((self.theMatrix[i][j]) == self.theMatrix[i][j + 1])
  96. and not (present[i][j + 1])):
  97. # Goes RIGHT
  98. j += 1
  99. continue
  100.  
  101. # Checks if the LEFT cell has the same element as current
  102. if (j > 0
  103. and (self.theMatrix[i][j] == self.theMatrix[i][j - 1])
  104. and not (present[i][j - 1])):
  105. # Goes LEFT
  106. j -= 1
  107. continue
  108.  
  109. # Checks if the DOWN cell has the same element as current
  110. if (i > 0
  111. and (self.theMatrix[i][j] == self.theMatrix[i - 1][j])
  112. and not (present[i - 1][j])):
  113. # Goes DOWN
  114. i -= 1
  115. continue
  116.  
  117. # Checks if the UP cell has the same element as current
  118. if (i < self.height - 1
  119. and (self.theMatrix[i][j] == self.theMatrix[i + 1][j])
  120. and not (present[i + 1][j])):
  121. # Goes UP
  122. i += 1
  123. continue
  124.  
  125. # Exit if there other direction to go
  126. break
  127.  
  128. # Return the path length of the given cell
  129. return sequence_length[start_row][start_col]
  130.  
  131. # Function for finding the longest path in the matrix
  132. def longest_sequence(self):
  133. # Setting the longest sequence to be 1
  134. longest_seq = 1
  135.  
  136. # Matrix for holding the length of every sequence_length
  137. sequence_length = [[-1 for i in range(self.width)]
  138. for i in range(self.height)]
  139.  
  140. # Goes through every cell in the matrix
  141. for i in range(self.height):
  142. for j in range(self.width):
  143. if sequence_length[i][j] == -1:
  144. # Boolean matrix whether the cell has been part of the path
  145. present = [[False for i in range(self.width)]
  146. for i in range(self.height)]
  147. # Finding the longest path from every cell
  148. sequence_length[i][j] = self.sequence(i, j, sequence_length, present)
  149. # Finding the longestSequence, so far
  150. longest_seq = max(longest_seq, sequence_length[i][j])
  151. # If the longest path is equal to the area that this means
  152. # there cannot be found longer path than this
  153. if longest_seq == self.area():
  154. break
  155. # Returns the result
  156. return longest_seq
  157.  
  158. # Creating a boolean variables
  159.  
  160.  
  161. is_test = None
  162.  
  163. # Checks if there are additional parameters
  164. if 1 < len(sys.argv) <= 5:
  165. # Sets the variable to True
  166. is_test = True
  167. for numOfParm in range(1, len(sys.argv)):
  168. # For every given parameter, it creates a matrix
  169. matrix = Matrix(sys.argv[numOfParm], is_test)
  170. # Print the result after calling the function for finding longest path
  171. print("The number of the longest adjacent sequence from "
  172. + sys.argv[numOfParm] + " is " + str(matrix.longest_sequence()))
  173. # This means there are no additional parameters
  174. elif len(sys.argv) == 1:
  175. # Sets the variable to True
  176. is_test = False
  177. # Getting the parameters of the wanted matrix
  178. first_line = input("Please, enter the size of the matrix (rows columns): ")
  179. # Creates a matrix with given parameters
  180. matrix = Matrix(first_line, is_test)
  181. # Print the result after calling the function for finding the longest path
  182. print("The number of the longest adjacent sequence is "
  183. + str(matrix.longest_sequence()))
  184. # There are more than 4 additional parameters
  185. else:
  186. print("The program accepts from one to four additional parameters!")
  187.  
Advertisement
Add Comment
Please, Sign In to add comment