Technical assignment

 avatar
unknown
python
4 years ago
2.3 kB
8
Indexable
#I used 2D array for the pyramid structure and gave 0 to the empty parts.
array = [[215,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [193,124,0,0,0,0,0,0,0,0,0,0,0,0,0], [117,237,442,0,0,0,0,0,0,0,0,0,0,0,0],
[218,935,417,235,0,0,0,0,0,0,0,0,0,0,0], [320,804,522,417,345,0,0,0,0,0,0,0,0,0,0], [229,601,723,835,133,124,0,0,0,0,0,0,0,0,0],
[248,202,277,433,207,263,257,0,0,0,0,0,0,0,0], [359,464,504,528,516,716,871,182,0,0,0,0,0,0,0],
[461,441,426,656,863,560,380,171,923,0,0,0,0,0,0], [381,348,573,533,447,632,387,176,975,449,0,0,0,0,0],
[223,711,445,645,245,543,931,532,937,541,444,0,0,0,0], [330,131,333,928,377,733,17,778,839,168,197,197,0,0,0],
[131,171,522,137,217,224,291,413,528,520,227,229,928,0,0], [223,626,34,683,839,53,627,310,713,999,629,817,410,121,0],
[924,622,911,233,325,139,721,218,253,223,107,233,230,124,233]]

#Finding maximum value in the array
array_maximum = 0
for i in range(len(array)):
    for j in range(len(array)):
        if(array_maximum < array[i][j]):
            array_maximum =  array[i][j]	

#Function to find whether the number is prime or not. If prime, assigns negative value to number if not, returns the number.
def find_prime(number):
    if(number < 2):
        return number
    if(number == 2):
        return -array_maximum
    for i in range(2, number):
        if (number % i == 0):
            return number
    return -array_maximum

#Function to find maximum between two numbers
def find_max(a,b):
    if(a<b):
        return b
    else:
        return a

#Assigning negative value for primes in the array
for i in range(len(array)):
    for j in range(len(array)): 
        array[i][j] = find_prime(array[i][j])

""" 
Starting from the second row of array and assigning row elements new sum which is calculated by adding element to the maximum 
of upper diagonal and upper element. 
For example:
1            1          1
8 4    -->   9 5   -->  9 5
6 8 9        6 8 9      15 17 14
"""
for i in range(1,len(array)):
    for j in range(len(array)): 
        array[i][j] = array[i][j] + find_max(array[i-1][j-1],array[i-1][j])


#Finding maximum value in the last row of the array
maximum = 0
i = len(array)-1
for j in range(len(array)):
    if(maximum < array[i][j]):
        maximum =  array[i][j]	

print(maximum)
Editor is loading...