class PyramidCalculator: n_list = list() def __init__(self, file): """ The initializer for the class Arguments: file -- a file for extracting numbers """ self.file = file self.extract_from_file() self.convert_to_int() def extract_from_file(self): """Store numbers from the file to a list line by line""" for line in self.file.readlines(): # self.n_list.append(line.split()) def convert_to_int(self): """Convert the stored numbers in each line to integer""" for i in range(len(self.n_list)): for j in range(len(self.n_list[i])): self.n_list[i][j] = int(self.n_list[i][j]) @staticmethod def check_prime(number): """ Check whether given number is a prime number Arguments: number -- any number to check """ for i in range(number): if i != 0 and i != 1: if number % i == 0: return False return True def check_greater(self, line, idx): """ Check whether which number is greater and not prime by using previous number's index to detect possible next two numbers. If both of the possible two numbers are prime, return -1 to stop the addition Arguments: line -- the line which we are deciding between two numbers idx -- the index of the previous number """ if line[idx] > line[idx + 1] and self.check_prime(line[idx]) is False: return line[idx] elif line[idx + 1] > line[idx] and self.check_prime(line[idx + 1]) is False: return line[idx + 1] elif self.check_prime(line[idx]) and self.check_prime(line[idx + 1]): return -1 else: if self.check_prime(line[idx]) is False: return line[idx] else: return line[idx + 1] def max_sum_possible(self): """ Print the possible maximum sum where all the numbers are the greatest and not prime as well as in diagonally or downwards order """ max_sum = self.n_list[0][0] line_num = 0 idx = 0 while line_num < len(self.n_list) - 1: temp_num = self.check_greater(self.n_list[line_num + 1], idx) if temp_num == -1: break else: max_sum += temp_num idx = self.n_list[line_num + 1].index(temp_num) line_num += 1 print(max_sum) sample_file = open("assignment.txt", "r") # opening file as read-only pyramid_calculator = PyramidCalculator(sample_file) # creating an object from the pyramid_calculator class pyramid_calculator.max_sum_possible()