Question: given an integer number n, print the numbers from 1 up to n2 like this:
n = 4
result is:
01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07
How do you solve it (apart from the solution provided in the link below)?
http://www.programmersheaven.com/mb/CandCPP/81986/81986/problem-in-making-ap-c++-program/?S=B20000
I'm looking in another direction. So far, I'm trying to figure out if I could obtain the ordered list of positions I have to fill in.
Here's what Im looking into: is there a way to obtain the "fdisp" so as to solve the problem that way, instead of "walk" in the matrix?
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
n = len(matrix)
# final disposition wrote by hand: how to get it for arbitrary n?
fdisp = [(0,0), (0,1), (0,2), (0,3), (1,3), (2,3), (3,3), (3,2),
(3,1), (3,0), (2,0), (1,0), (1,1), (1,2), (2,2), (2,1)]
for val,i in enumerate(fdisp):
matrix[i[0]][i[1]] = val + 1
def show_matrix(matrix, n):
for i,l in enumerate(matrix):
for j in range(n):
print "%d\t" % matrix[i][j],
print
show_matrix(matrix, n)