Created
December 5, 2018 17:44
-
-
Save hansrajdas/0e9b017b9a0417074241528c2a43b349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# The following code is buggy. Find and fix the bugs here to make it pass the test cases. | |
# Re-writing the complete code (or core logic) will *not* be accepted. | |
import sys, os | |
def get_layer(array, row, col, width, height): | |
ans = [] | |
for c in range(col, col+width-1): | |
ans.append(array[row][c]) | |
for r in range(row, row+height-1): | |
ans.append(array[r][col+width-1]) | |
for c in range(col+width-1, col, -1): | |
ans.append(array[row+height-1][c]) | |
for r in range(row+height-1, row, -1): | |
ans.append(array[r][col]) | |
return ans | |
def get_input(): | |
inp = sys.stdin.readline() | |
return inp.strip().split() | |
if __name__=='__main__': | |
nm = get_input() | |
N = int(nm[0]) | |
M = int(nm[1]) | |
array = [] | |
for i in xrange(N): | |
line = get_input() | |
array.append(line) | |
row = col = 0 | |
width = M | |
height = N | |
answer = [] | |
if height != len(array) or width != len(array[0]): | |
print "Wrong input" | |
else: | |
while width > 1 and height > 1: | |
answer.extend(get_layer(array, row, col, width, height)) | |
row += 1 | |
col += 1 | |
width -= 2 | |
height -= 2 | |
if width == 1 and height == 1: | |
answer.append(array[N/2][M/2]) | |
if width > 1 and height == 1: | |
for c in range(col, col+width): | |
answer.append(array[row][c]) | |
elif height > 1 and width == 1: | |
for r in range(row, row+height): | |
answer.append(array[r][col]) | |
print ' '.join(answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment