Given a square matrix and a number k. The task is to shift the first k elements of each row to the right of the matrix.
Input:
Matrx[N][N] = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
k = 2
Output:
Matrx[N][N] = [[3, 1, 2]
[6, 4, 5]
[9, 7, 8]]
def shiftMatrx(Matrx,K): if(K>len(Matrx)): print("Shifting Not Possible!!!") return i,L=0,[] while(i<len(Matrx)): temp=[] for j in range(K,len(Matrx)): temp.append(Matrx[i][j]) for j in range(0,K): temp.append(Matrx[i][j]) i+=1 L.append(temp) return L #Driver ''' Matrx = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] ''' Matrx=[] while True: arr=list(map(str,input().split(" "))) if arr != ['']: Matrx.append(list(map(int,arr))) else: break K=int(input("K: ")) S_Matx = shiftMatrx(Matrx,K) for i in S_Matx: print (i)
INPUT_1:
1 2 3
4 5 6
7 8 9
K: 2
OUTPUT:
[3, 1, 2]
[6, 4, 5]
[9, 7, 8]
INPUT_2:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
K: 2
OUTPUT:
[3, 4, 1, 2]
[7, 8, 5, 6]
[11, 12, 9, 10]
[15, 16, 13, 14]
ILLUSTRATION
Morae Q!
- Find the length of the array’s longest increasing sub-sequence.
- Arrange numbers in a circle so that any two neighbouring numbers differ by 1.
- Find partial names and count the total numbers.
- Find the minimized sum of non-deleted elements of the array after the end of the game.
- Find the maximum number of good sleeping times optimally.
- Sort the elements of the array in the order in which they are required.
- Find the minimum number of flats, monkey needs to visit to catch ninjas.
- Convert the square matrix to matrix in Z form.
- Shift the K elements of each row to right of the matrix.
- Find maximum possible number of students in a balanced team with skills.
- Find the Maximum number of pairs of points you can match with each other.
- Calculate the number of non-empty good subarrays of given array.
- Transform the binary string A into the string B using finite operations.
- Find the number of potion must the character take to jump the hurdles.
- Count the total number of vowels and consonants.
- Return all elements of the matrix in spiral order.
- Return all palindromic paths of the matrix.
- Write the code to change the display in reverse order using pointer.
- Find the number of days the expedition can last.
- Find the minimum size of the sub-segment to make the array pairwise distinct.