Plant Growth Calculation

Plant Growth Calculation

In a matrix containing R rows and C columns, various plants are planted. The growth of a given plant in a calendar month depends on it's current height at the beginning of the month. If the current height is a prime number, the height by which the plant grows in that month is equal to the current height minus previous prime number (If there is no previous prime number consider the previous prime number as zero). If the current height is not a prime number then the height by which the plant grows in that month is equal to the sum of the unit digits of the adjacent plants height at the beginning of the month (If there is no adjacent plant to left or right consider the value as zero). Given the current height of the plants at the beginning of first month, print the height of the plants after N months. 

Input Format: 

The first line contains R and C separated by a space. Next R lines contains C values representing the height of the plants (with the values separated by a space). Last line contains N. 

Output Format: 

R lines containing C values denoting the height of the plants after N months (with the values separated by a space). 

Boundary Conditions: 

1 <= R, C <= 999 

1 <= N <= 100 

1 <= Initial height of the plants <= 100 

Example Input/Output 1: 

Input: 

3 3 

1 2 4 

6 1 11 

5 7 12 

Output: 

4 13 10

9 20 23 

9 25 21 

Explanation: 

After the 1st month, the height of the plants will be 

3 4 6 

7 8 15 

7 9 19 

After 2nd month, the height of the plants will be 

4 13 10 

9 20 23 

9 25 21

Program:

def isprime(x): 
    if x == 1:
        return 0
    for i in range(2int(x**0.5)+1): 
        if x%i==0:
            return 0 
    return 1        

n,m = map(int,input().split())
l = [] 
for i in range(n): 
    l.append(list(map(int,input().split())))
k = int(input()) 
for i in range(k): 
    q = []
    for j in range(n): 
        w = []
        for z in range(m): 
            if isprime(l[j][z]): 
                prevprime = l[j][z] - 1 
                while prevprime >0 and not isprime(prevprime):
                    prevprime -= 1 
                w.append(l[j][z] + (l[j][z] - prevprime))
            else
                sum_adj = 0 
                if z-1 >= 0:
                    sum_adj += l[j][z-1] % 10 
                if z + 1 < m
                    sum_adj += l[j][z+1] % 10 
                w.append(l[j][z] + sum_adj)
        q.append(w)
    l = q.copy()  

for i in l:
    print(*i)

Comments

Popular posts from this blog

Pronic Integers in N - InfyTQ question

Count Strong Points - Accenture

Letters at position of multiples of a number