Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- T = int(input())
- solved_grids = []
- # Store your solved grids here for easy printing
- i = 0
- while i < T:
- # Get input
- # rcMN = input()
- # print(rcMN)
- # r, c, M, N = rcMN.split() # splitting input
- # protip:
- r, c, M, N = map(int, input().split())
- # this is basically
- # string = input()
- # r = int(string.split()[0])
- # c = int(string.split()[1])
- # M = int(string.split()[2])
- # N = int(string.split()[3])
- G = []
- # j = 0
- # arrange horizontally
- # G1 = []
- # no need for extra storage if you can do the work on the variable itself
- # for x in range(int(r)):
- # line = input().split(" ")
- # # G1.append(line)
- # G.append(line)
- # btw, another protip:
- G = [input().split(" ") for _ in range(r)]
- # list comprehension is a thing. I recommend you take a look at it
- # Don't overuse it though.
- # The point is not to make your code shorter, it's to make it simpler.
- # Sure you have 1 line of code for a solution but could you really get it
- # the first time you've read it?
- # rotate
- # G2 = []
- # no need for an extra array
- # for each element in g
- for x in range(len(G)):
- # G2a = G1[x][int(M) - 1:] + G1[x][:int(M) - 1] # slices the list at M
- # G2.append(G2a)
- G[x] = G[x][-(M % c):] + G[x][:-(M % c)]
- # Notice the modulo? What are we gonna do if we shift a 5-element array 6 units to the right?
- # 11 units to the right?
- # arrange vertically
- """G3 = []
- for x in range(int(c)):
- newrow = []
- for y in range(int(r)):
- z = G2[y][x]
- newrow.append(int(z))
- G3.append(newrow) """
- # switcheroo
- # G4 = []
- # for x in range(len(G2)):
- # G4a = G2[x][int(N):] + G2[x][:int(N)] # slices the string at M
- # G4.append(G4a)
- # print(G4[2][0])
- # print(G4)
- # you know, if you think about it, shifting elements up is literally
- # shifting the rows up, which means you're shifting whole lists.
- # ...if you think about it you're actually shifting the lists
- # in the grid LEFTWARDS.
- G = G[(N % r):] + G[:(N % r)]
- # grid solved!
- solved_grids.append(G)
- # arranging output
- # traverse all sublists and append the kth element of each
- # c1 = 0
- # while c1 < len(G4):
- # newer_row = []
- # c2 = 0
- # while c2 < len(G4[c1]):
- # newer_row.append(G4[c1][c2])
- # c2 += 1
- # print(newer_row)
- # c1 += 1
- i += 1
- # Look up for each
- # All for loops in python are for each loops.
- for grid in solved_grids:
- for row in grid:
- print(' '.join(row))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement