Skip to content

Instantly share code, notes, and snippets.

@kamath
Created January 26, 2017 02:08
Show Gist options
  • Save kamath/cba5a1215567996927c35a0feaa0793b to your computer and use it in GitHub Desktop.
Save kamath/cba5a1215567996927c35a0feaa0793b to your computer and use it in GitHub Desktop.
# Enter your code here. Read input from STDIN. Print output to STDOUT
def rotate(t, m, n, a):
left = [i[0] for i in a[:-1]]
right = [i[-1] for i in a[1:]]
a[0] = a[0][1:]+[right[0]]
a[-1] = [left[-1]]+a[-1][:-1]
for i,x in enumerate(a):
if i < len(a) - 1:
a[i+1][0] = left[i]
if i > 0:
a[i-1][-1] = right[i-1]
if len(t) != len(a):
for i, x in enumerate(t[1:-1]):
t[i+1] = [x[0]]+a[i]+[x[-1]]
if len(a) - 2 <= 0:
return t
else:
a = [i[1:-1] for i in a]
return rotate(t,m,n,a[1:-1])
m,n,r = map(int, raw_input().split(' '))
a = []
for i in range(n):
a.append(map(int, raw_input().split(' ')))
for i in range(r):
arr = rotate(a,m,n,a)
for a in arr:
print ' '.join(map(str, a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment