Reverse Column - First and Last

 Coding Problem Keys 

 Reverse Column - First and Last 

 Problem Statement 

The program must accept an integer matrix of size RxC as the input. The program must reverse all the elements in the column if the first and last elements of the column are same. Then the program must print the modified matrix as the output.

 Input Format 

The first line contains the value of R and C separated by space.
The next R lines contain each C elements separated by space(s).

 Output Format 

The first R lines contain each C elements separated by space.

 Boundary Condition(s) 

2 <= R, C <= 50
-999 <= Each element of the matrix <= 999

 Example Input/Output 1 

 Input 

5 4
11 7 5 19
4 13 16 10
9 9 13 0
7 5 17 8
11 8 5 12

 Output 

11 7 5 19
7 13 17 10
9 9 13 0
4 5 16 8
11 8 5 12

 Explanation 

The elements in the first column are 11, 4, 9, 7 and 11. Here the first and the last elements are same. So reverse the elements in the first column.

The elements in the third column are 5, 17, 13, 16 and 5. Here the first and the last elements are same. So reverse the elements in the third column.

Hence the output is

11 7 5 19
7 13 17 10
9 9 13 0
4 5 16 8
11 8 5 12

 Example Input/Output 2 

 Input 

4 4
49 81 22 92
20 60 14 79
57 39 20 73
50 81 22 92

 Output 

49 81 22 92
20 39 20 73
57 60 14 79
50 81 22 92

 Max Execution Time Limit: 4000 millisecs 

 Solution 

 Programming Language: Python 3 Language 

r,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(r)]
k=[list(i) for i in zip(*l)]
for i in range(len(k)):
    if k[i][0]==k[i][-1]:
        k[i]=k[i][::-1]
h=[list(i) for i in zip(*k)]
for i in h:
    print(*i)

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Snakes and Ladders

Difference between a number and it's reverse

Find Co-ordinates - Other Side

Sum of N Integers - Even Reversed

Sort the Binary Values

Matrix Diagonal Pattern