Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Python: 3.6
- Version: 1.2
- Author: Hanzallah Azim Burney
- """
- '''
- The method checks to see if a number is prime or not
- parameters: number - the number which needs to be checked for whether or not it is prime
- return: boolean - true if number is prime else false
- '''
- def isPrime(number):
- if (number <= 1):
- return False
- for i in range (2, number):
- if (number % i == 0):
- return False
- return True
- '''
- The method calculates using recursion the maximum sum of non-prime numbers in an orthogonal triangle
- parameters: array - the orthogonal triangle which needs to be parsed
- rows - the number of rows in the orthogonal triangle
- currCol - the current column position
- nextRow - reduction of row number to reach the base case
- return: int - the maximum sum of non-prime numbers in an orthogonal triangle
- '''
- def maxSumFunc(array, rows, currCol, nextRow):
- if nextRow == 0:
- if isPrime(array[rows-nextRow][currCol]) == False:
- return array[rows-nextRow][currCol]
- else:
- return 0
- else:
- curr = array[rows-nextRow][currCol]
- if isPrime(curr) == False:
- s1 = curr + maxSumFunc(array, rows, currCol, nextRow-1)
- s2 = curr + maxSumFunc(array, rows, currCol+1, nextRow-1)
- s3 = curr + maxSumFunc(array, rows, currCol-1, nextRow-1)
- if s1 >= s2 and s1 >= s3:
- return s1
- elif s2 > s1 and s2 >= s3:
- return s2
- else:
- return s3
- else:
- return 0
- '''
- The method calls the maxSumFunc and initializes the method with values
- parameters: arr - the orthogonal triangle which needs to be parsed
- rows - the number of rows in the orthogonal triangle
- return: int - the maximum sum of non-prime numbers in an orthogonal triangle
- '''
- def maxSumCaller(arr, row):
- return maxSumFunc(arr, row-1, 0, row-1)
- name = input("Enter file source: ")
- # Read file and set then in a 2-D list
- array = []
- rows = 0
- with open(name, "r") as ins:
- for line in ins:
- rows += 1
- temp = [int(x) for x in line.split()]
- array.append(temp)
- # Finally call the maxSumCaller and display the sum
- print("The maximum sum of the orthogonal triangle is",maxSumCaller(array,rows))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement