Advertisement
Guest User

PI Works

a guest
Jan 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Python: 3.6
  4. Version: 1.2
  5. Author: Hanzallah Azim Burney
  6. """
  7.  
  8. '''
  9. The method checks to see if a number is prime or not
  10. parameters: number - the number which needs to be checked for whether or not it is prime
  11. return: boolean - true if number is prime else false
  12. '''
  13. def isPrime(number):
  14.     if (number <= 1):
  15.         return False
  16.    
  17.     for i in range (2, number):
  18.         if (number % i == 0):
  19.             return False
  20.     return True
  21.  
  22. '''
  23. The method calculates using recursion the maximum sum of non-prime numbers in an orthogonal triangle
  24. parameters: array - the orthogonal triangle which needs to be parsed
  25.             rows - the number of rows in the orthogonal triangle
  26.             currCol - the current column position
  27.             nextRow - reduction of row number to reach the base case
  28. return: int - the maximum sum of non-prime numbers in an orthogonal triangle
  29. '''
  30. def maxSumFunc(array, rows, currCol, nextRow):
  31.     if nextRow == 0:
  32.         if isPrime(array[rows-nextRow][currCol]) == False:
  33.             return array[rows-nextRow][currCol]
  34.         else:
  35.             return 0
  36.     else:
  37.         curr = array[rows-nextRow][currCol]
  38.         if isPrime(curr) == False:
  39.             s1 = curr +  maxSumFunc(array, rows, currCol, nextRow-1)
  40.             s2 = curr + maxSumFunc(array, rows, currCol+1, nextRow-1)
  41.             s3 = curr + maxSumFunc(array, rows, currCol-1, nextRow-1)      
  42.            
  43.             if s1 >= s2 and s1 >= s3:
  44.                 return s1
  45.             elif s2 > s1 and s2 >= s3:
  46.                 return s2
  47.             else:
  48.                 return s3
  49.         else:
  50.             return 0
  51.  
  52. '''
  53. The method calls the maxSumFunc and initializes the method with values
  54. parameters: arr - the orthogonal triangle which needs to be parsed
  55.             rows - the number of rows in the orthogonal triangle
  56. return: int - the maximum sum of non-prime numbers in an orthogonal triangle
  57. '''
  58. def maxSumCaller(arr, row):
  59.     return maxSumFunc(arr, row-1, 0, row-1)
  60.    
  61.  
  62. name = input("Enter file source: ")
  63.  
  64. # Read file and set then in a 2-D list
  65. array = []
  66. rows = 0
  67. with open(name, "r") as ins:
  68.     for line in ins:
  69.         rows += 1
  70.         temp = [int(x) for x in line.split()]      
  71.         array.append(temp)
  72. # Finally call the maxSumCaller and display the sum
  73. print("The maximum sum of the orthogonal triangle is",maxSumCaller(array,rows))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement