nikhil-and-matrice-1

Nikhil got to know about what is a matrice. He made several patterns using 1-d and 2-d matrices. One of which was his favorite pattern. Your task is to right a code for his favorite pattern.

NOTE :- To understand his favorite pattern see the sample output

Input:-

First line of code takes number of test cases 'N' as its input. The following N lines take size 'S' of a sqaure matrice as its input.

Output:-

Print Nikhils favorite pattern and seperate two different test cases with an empty line.

Constraint:-

N ≤ 10

S ≤ 10

SAMPLE INPUT
1
3

SAMPLE OUTPUT
123
894
765

Source: hackerearth | Difficulty: EASY


Solutions:
#!/usr/bin/env python
 
def number_spiral(h, w):
	# total number of elements in array
	n = w * h
 
	# start at top left (row 0 column 0)
	row, column = 0,0
 
	# first move is on the same row, to the right
	d_row, d_column = 0, 1
 
	# fill 2d array with dummy values we'll overwrite later
	arr = [[None ]* w for z in range(h)]
 
	for i in range(1,n+1):
		arr[row][column] = i
 
		# next row and column
		nrow, ncolumn = row + d_row, column + d_column
 
		if 0 <= nrow < h and 0 <= ncolumn < w and arr[nrow][ncolumn] == None:
			# no out of bounds accesses or overwriting already-placed elements.
			row, column = nrow, ncolumn
		else:
			# change direction
			d_row , d_column = d_column, -d_row
			row, column = row + d_row, column + d_column
 
	# print it out!
	for a in range(h):
		ll = ""
		for b in range(w):
			ll += str(arr[a][b])
		print(ll)
 

for _ in range(int(input())):
  s = int(input())
  number_spiral(s, s)
  print()