Answer to Question #262108 in Python for prime

Question #262108

For this program I want an exact output as I given in the sample numbers position should not be changed.


M, N = input('Enter M, N: ').split()

M, N = int(M), int(N)

matrix = []

for _ in range(M):

row = [int(x) for x in input().split()]

matrix.append(row)

K = int(input('Enter K: '))

values = matrix[0][:-1] + [x[-1] for x in matrix][:-1] + matrix[-1][::-1][:-1] + [x[0] for x in matrix][::-1][:-1]

values = values[-K:] + values[:-K]

output = matrix

idxs = [(0, j) for j in range(N)][:-1] + [(i, N - 1) for i in range(M)][:-1] + [(M - 1, j) for j in range(N)][::-1][:-1] + [(i, 0) for i in range(M)][::-1][:-1]

idx = 0

for i, j in idxs:

output[i][j] = values[idx]

idx += 1

for i in output:

for j in i:

print(j, end=' ')

print()


Sample Input 1

4 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

3

Sample Output 1

13 9 5 1

14 7 11 2

15 6 10 3

16 12 8 4

Sample Input 2

3 4

1 2 3 4

10 11 12 5

9 8 7 6

2

Sample Output 2

9 10 1 2

8 11 12 3

7 6 5 4


1
Expert's answer
2021-11-08T05:33:03-0500
# A Dynamic Programming based Python program for edit 
# distance problem 

  

  

def editDistDP(str1, str2, m, n): 


    # Create a table to store results of subproblems 


    dp = [[0 for x in range(n + 1)] for x in range(m + 1)] 


  

    # Fill d[][] in bottom up manner 


    for i in range(m + 1): 


        for j in range(n + 1): 


  

            # If first string is empty, only option is to 


            # insert all characters of second string 


            if i == 0: 


                dp[i][j] = j    # Min. operations = j 


  

            # If second string is empty, only option is to 


            # remove all characters of second string 


            elif j == 0: 


                dp[i][j] = i    # Min. operations = i 


  

            # If last characters are same, ignore last char 


            # and recur for remaining string 


            elif str1[i-1] == str2[j-1]: 


                dp[i][j] = dp[i-1][j-1] 


  

            # If last character are different, consider all 


            # possibilities and find minimum 


            else: 


                dp[i][j] = 1 + min(dp[i][j-1],        # Insert 


                                   dp[i-1][j],        # Remove 


                                   dp[i-1][j-1])    # Replace 


  

    return dp[m][n] 


  

  
# Driver code 

str1 = "sunday"


str2 = "saturday"


  

print(editDistDP(str1, str2, len(str1), len(str2)))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS