RosettaCodeData/Task/Zig-zag-matrix/Phix/zig-zag-matrix-2.phix
2016-12-05 23:44:36 +01:00

17 lines
393 B
Text

integer n = 5
string fmt = sprintf("%%%dd",length(sprintf("%d",n*n-1)))
sequence m = repeat(repeat("??",n),n)
integer x = 1, y = 1
for d=0 to n*n-1 do
m[y][x] = sprintf(fmt,d)
if mod(x+y,2) then
{x,y} = iff(y<n?{x-(x>1),y+1}:{x+1,y})
else
{x,y} = iff(x<n?{x+1,y-(y>1)}:{x,y+1})
end if
end for
for i=1 to n do
m[i] = join(m[i])
end for
puts(1,join(m,"\n"))