Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,50 +0,0 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Zig_Zag is
type Matrix is array (Positive range <>, Positive range <>) of Natural;
function Zig_Zag (Size : Positive) return Matrix is
Data : Matrix (1..Size, 1..Size);
I, J : Integer := 1;
begin
Data (1, 1) := 0;
for Element in 1..Size**2 - 1 loop
if (I + J) mod 2 = 0 then
-- Even stripes
if J < Size then
J := J + 1;
else
I := I + 2;
end if;
if I > 1 then
I := I - 1;
end if;
else
-- Odd stripes
if I < Size then
I := I + 1;
else
J := J + 2;
end if;
if J > 1 then
J := J - 1;
end if;
end if;
Data (I, J) := Element;
end loop;
return Data;
end Zig_Zag;
procedure Put (Data : Matrix) is
begin
for I in Data'Range (1) loop
for J in Data'Range (2) loop
Put (Integer'Image (Data (I, J)));
end loop;
New_Line;
end loop;
end Put;
begin
Put (Zig_Zag (5));
end Test_Zig_Zag;

View file

@ -3,35 +3,35 @@ set n to 5 -- Size of zig-zag matrix (n^2 cells).
-- Create an empty matrix.
set m to {}
repeat with i from 1 to n
set R to {}
repeat with j from 1 to n
set end of R to 0
end repeat
set end of m to R
set R to {}
repeat with j from 1 to n
set end of R to 0
end repeat
set end of m to R
end repeat
-- Populate the matrix in a zig-zag manner.
set {x, y, v, d} to {1, 1, 0, 1}
repeat while v < (n ^ 2)
if 1 x and x n and 1 y and y n then
set {m's item y's item x, x, y, v} to {v, x + d, y - d, v + 1}
else if x > n then
set {x, y, d} to {n, y + 2, -d}
else if y > n then
set {x, y, d} to {x + 2, n, -d}
else if x < 1 then
set {x, y, d} to {1, y, -d}
else if y < 1 then
set {x, y, d} to {x, 1, -d}
end if
if 1 x and x n and 1 y and y n then
set {m's item y's item x, x, y, v} to {v, x + d, y - d, v + 1}
else if x > n then
set {x, y, d} to {n, y + 2, -d}
else if y > n then
set {x, y, d} to {x + 2, n, -d}
else if x < 1 then
set {x, y, d} to {1, y, -d}
else if y < 1 then
set {x, y, d} to {x, 1, -d}
end if
end repeat
--> R = {{0, 1, 5, 6, 14}, {2, 4, 7, 13, 15}, {3, 8, 12, 16, 21}, {9, 11, 17, 20, 22}, {10, 18, 19, 23, 24}}
-- Reformat the matrix into a table for viewing.
repeat with i in m
repeat with j in i
set j's contents to (characters -(length of (n ^ 2 as string)) thru -1 of (" " & j)) as string
end repeat
set end of i to return
repeat with j in i
set j's contents to (characters -(length of (n ^ 2 as string)) thru -1 of (" " & j)) as string
end repeat
set end of i to return
end repeat
return return & m as string

View file

@ -2,33 +2,33 @@ set n to 5
set m to {}
repeat with i from 1 to n
set end of m to {} -- Built a foundation for the matrix out of n empty lists.
set end of m to {} -- Built a foundation for the matrix out of n empty lists.
end repeat
set {v, d, i} to {0, -1, 1}
repeat while v < n ^ 2
if length of m's item i < n then
set {end of m's item i, i, v} to {f(v, n), i + d, v + 1}
if i < 1 then
set {i, d} to {1, -d}
else if i > n then
set {i, d} to {n, -d}
else if i > 1 and (count of m's item (i - 1)) = 1 then
set d to -d
end if
else
set {i, d} to {i + 1, 1}
end if
if length of m's item i < n then
set {end of m's item i, i, v} to {f(v, n), i + d, v + 1}
if i < 1 then
set {i, d} to {1, -d}
else if i > n then
set {i, d} to {n, -d}
else if i > 1 and (count of m's item (i - 1)) = 1 then
set d to -d
end if
else
set {i, d} to {i + 1, 1}
end if
end repeat
-- Handler/function to format the cells on the fly.
on f(v, n)
return (characters -(length of (n ^ 2 as string)) thru -1 of (" " & v)) as string
return (characters -(length of (n ^ 2 as string)) thru -1 of (" " & v)) as string
end f
-- Reformat the matrix into a table for viewing.
set text item delimiters to ""
repeat with i in m
set i's contents to (i as string) & return
set i's contents to (i as string) & return
end repeat
return return & m as string

View file

@ -4,15 +4,14 @@ zigzag: function [n][
x: 1, y: 1, v: 0, d: 1
while [v < n^2][
if? all? @[1 =< x x =< n 1 =< y y =< n][
switch all? @[1 =< x x =< n 1 =< y y =< n][
set get result (y-1) (x-1) v
x: x + d, y: y - d, v: v + 1
]
else[if? x > n [x: n, y: y + 2, d: neg d]
else[if? y > n [x: x + 2, y: n, d: neg d]
else[if? x < 1 [x: 1, d: neg d]
else[if y < 1 [y: 1, d: neg d]
]
[switch x > n [x: n, y: y + 2, d: neg d]
[switch y > n [x: x + 2, y: n, d: neg d]
[switch x < 1 [x: 1, d: neg d]
[if y < 1 [y: 1, d: neg d]]
]
]
]

View file

@ -1,27 +0,0 @@
#include <Array.au3>
$Array = ZigZag(5)
_ArrayDisplay($Array)
Func ZigZag($int)
Local $av_array[$int][$int]
Local $x = 1, $y = 1
For $I = 0 To $int ^ 2 -1
$av_array[$x-1][$y-1] = $I
If Mod(($x + $y), 2) = 0 Then ;Even
if ($y < $int) Then
$y += 1
Else
$x += 2
EndIf
if ($x > 1) Then $x -= 1
Else ; ODD
if ($x < $int) Then
$x += 1
Else
$y += 2
EndIf
If $y > 1 Then $y -= 1
EndIf
Next
Return $av_array
EndFunc ;==>ZigZag

View file

@ -1,50 +1,50 @@
beads 1 program 'Zig-zag Matrix'
calc main_init
var test : array^2 of num = create_array(5)
printMatrix(test)
var test : array^2 of num = create_array(5)
printMatrix(test)
calc create_array(
dimension:num
):array^2 of num
var
result : array^2 of num
lastValue = dimension^2 - 1
loopFrom
loopTo
row
col
currDiag = 0
currNum = 0
loop
if (currDiag < dimension) // if doing the upper-left triangular half
loopFrom = 1
loopTo = currDiag + 1
else // doing the bottom-right triangular half
loopFrom = currDiag - dimension + 2
loopTo = dimension
loop count:c from:loopFrom to:loopTo
var i = loopFrom + c - 1
if (rem(currDiag, 2) == 0) // want to fill upwards
row = loopTo - i + loopFrom
col = i
else // want to fill downwards
row = i
col = loopTo - i + loopFrom
result[row][col] = currNum
inc currNum
inc currDiag
if (currNum > lastValue)
exit
return result
dimension:num
):array^2 of num
var
result : array^2 of num
lastValue = dimension^2 - 1
loopFrom
loopTo
row
col
currDiag = 0
currNum = 0
loop
if (currDiag < dimension) // if doing the upper-left triangular half
loopFrom = 1
loopTo = currDiag + 1
else // doing the bottom-right triangular half
loopFrom = currDiag - dimension + 2
loopTo = dimension
loop count:c from:loopFrom to:loopTo
var i = loopFrom + c - 1
if (rem(currDiag, 2) == 0) // want to fill upwards
row = loopTo - i + loopFrom
col = i
else // want to fill downwards
row = i
col = loopTo - i + loopFrom
result[row][col] = currNum
inc currNum
inc currDiag
if (currNum > lastValue)
exit
return result
calc printMatrix(
matrix:array^2 of num
)
var dimension = tree_count(matrix)
var maxDigits = 1 + lg((dimension^2-1), base:10)
loop across:matrix ptr:rowp index:row
var tempstr : str
loop across:rowp index:col
tempstr = tempstr & " " & to_str(matrix[row][col], min:maxDigits)
log(tempstr)
matrix:array^2 of num
)
var dimension = tree_count(matrix)
var maxDigits = 1 + lg((dimension^2-1), base:10)
loop across:matrix ptr:rowp index:row
var tempstr : str
loop across:rowp index:col
tempstr = tempstr & " " & to_str(matrix[row][col], min:maxDigits)
log(tempstr)

View file

@ -1,8 +1,8 @@
#include <vector>
#include <memory> // for auto_ptr
#include <cmath> // for the log10 and floor functions
#include <memory> // for auto_ptr
#include <cmath> // for the log10 and floor functions
#include <iostream>
#include <iomanip> // for the setw function
#include <iomanip> // for the setw function
using namespace std;
@ -11,71 +11,71 @@ typedef vector< IntRow > IntTable;
auto_ptr< IntTable > getZigZagArray( int dimension )
{
auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
dimension, IntRow( dimension ) ) );
auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
dimension, IntRow( dimension ) ) );
// fill along diagonal stripes (oriented as "/")
int lastValue = dimension * dimension - 1;
int currNum = 0;
int currDiag = 0;
int loopFrom;
int loopTo;
int i;
int row;
int col;
do
{
if ( currDiag < dimension ) // if doing the upper-left triangular half
{
loopFrom = 0;
loopTo = currDiag;
}
else // doing the bottom-right triangular half
{
loopFrom = currDiag - dimension + 1;
loopTo = dimension - 1;
}
// fill along diagonal stripes (oriented as "/")
int lastValue = dimension * dimension - 1;
int currNum = 0;
int currDiag = 0;
int loopFrom;
int loopTo;
int i;
int row;
int col;
do
{
if ( currDiag < dimension ) // if doing the upper-left triangular half
{
loopFrom = 0;
loopTo = currDiag;
}
else // doing the bottom-right triangular half
{
loopFrom = currDiag - dimension + 1;
loopTo = dimension - 1;
}
for ( i = loopFrom; i <= loopTo; i++ )
{
if ( currDiag % 2 == 0 ) // want to fill upwards
{
row = loopTo - i + loopFrom;
col = i;
}
else // want to fill downwards
{
row = i;
col = loopTo - i + loopFrom;
}
for ( i = loopFrom; i <= loopTo; i++ )
{
if ( currDiag % 2 == 0 ) // want to fill upwards
{
row = loopTo - i + loopFrom;
col = i;
}
else // want to fill downwards
{
row = i;
col = loopTo - i + loopFrom;
}
( *zigZagArrayPtr )[ row ][ col ] = currNum++;
}
( *zigZagArrayPtr )[ row ][ col ] = currNum++;
}
currDiag++;
}
while ( currDiag <= lastValue );
currDiag++;
}
while ( currDiag <= lastValue );
return zigZagArrayPtr;
return zigZagArrayPtr;
}
void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
size_t dimension = zigZagArrayPtr->size();
size_t dimension = zigZagArrayPtr->size();
int fieldWidth = static_cast< int >( floor( log10(
static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;
int fieldWidth = static_cast< int >( floor( log10(
static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;
size_t col;
for ( size_t row = 0; row < dimension; row++ )
{
for ( col = 0; col < dimension; col++ )
cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
cout << endl;
}
size_t col;
for ( size_t row = 0; row < dimension; row++ )
{
for ( col = 0; col < dimension; col++ )
cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
cout << endl;
}
}
int main()
{
printZigZagArray( getZigZagArray( 5 ) );
printZigZagArray( getZigZagArray( 5 ) );
}

View file

@ -3,21 +3,21 @@
int main(int c, char **v)
{
int i, j, m, n, *s;
int i, j, m, n, *s;
/* default size: 5 */
if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
/* default size: 5 */
if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
/* alloc array*/
s = malloc(sizeof(int) * m * m);
/* alloc array*/
s = malloc(sizeof(int) * m * m);
for (i = n = 0; i < m * 2; i++)
for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
for (i = n = 0; i < m * 2; i++)
for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
printf("%3d", s[i]);
for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
printf("%3d", s[i]);
/* free(s) */
return 0;
/* free(s) */
return 0;
}

View file

@ -1,50 +1,50 @@
class ZigZag(Integer size) {
value data = Array {
for (i in 0:size)
Array.ofSize(size, 0)
};
variable value i = 1;
variable value j = 1;
for (element in 0 : size^2) {
data[j - 1]?.set(i - 1, element);
if ((i + j).even) {
if (j < size) {
j++;
}
else {
i += 2;
}
if (i > 1) {
i--;
}
}
else {
if (i < size) {
i++;
}
else {
j += 2;
}
if (j > 1) {
j--;
}
}
}
shared void display() {
for (row in data) {
for (element in row) {
process.write(element.string.pad(3));
}
print(""); //newline
}
}
value data = Array {
for (i in 0:size)
Array.ofSize(size, 0)
};
variable value i = 1;
variable value j = 1;
for (element in 0 : size^2) {
data[j - 1]?.set(i - 1, element);
if ((i + j).even) {
if (j < size) {
j++;
}
else {
i += 2;
}
if (i > 1) {
i--;
}
}
else {
if (i < size) {
i++;
}
else {
j += 2;
}
if (j > 1) {
j--;
}
}
}
shared void display() {
for (row in data) {
for (element in row) {
process.write(element.string.pad(3));
}
print(""); //newline
}
}
}
shared void run() {
value zz = ZigZag(5);
zz.display();
value zz = ZigZag(5);
zz.display();
}

View file

@ -3,14 +3,14 @@
(when-let [n (first sizes)]
(when-let [s (seq coll)]
(cons (take n coll)
(partitions (next sizes) (drop n coll)))))))
(partitions (next sizes) (drop n coll)))))))
(defn take-from [n colls]
(lazy-seq
(when-let [s (seq colls)]
(let [[first-n rest-n] (split-at n s)]
(cons (map first first-n)
(take-from n (concat (filter seq (map rest first-n)) rest-n)))))))
(take-from n (concat (filter seq (map rest first-n)) rest-n)))))))
(defn zig-zag [n]
(->> (partitions (concat (range 1 (inc n)) (range (dec n) 0 -1)) (range (* n n)))

View file

@ -1,5 +1,5 @@
def zigzag(n)
(seq=(0...n).to_a).product(seq)
(seq=(0...n).to_a).cartesian_product(seq)
.sort_by {|x,y| [x+y, (x+y).even? ? y : -y]}
.map_with_index{|v, i| {v, i}}.sort.map(&.last).each_slice(n).to_a
end

View file

@ -9,7 +9,7 @@ int[][] zigZag(in int n) pure nothrow {
auto result = new typeof(return)(n, n);
foreach (immutable i, immutable p; L)
result[p.y][p.x] = i;
result[p.y][p.x] = cast(int) i;
return result;
}

View file

@ -9,12 +9,12 @@ var S: string;
begin
S:='';
for Y:=0 to High(Mat[0]) do
begin
S:=S+'[';
for X:=0 to High(Mat) do
S:=S+Format('%4.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
begin
S:=S+'[';
for X:=0 to High(Mat) do
S:=S+Format('%4.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
@ -23,34 +23,34 @@ var Mat: TMatrix;
var X,Y,Inx,Dir: integer;
const Size = 10;
procedure Toggle(var I: integer);
{Toggle Direction and increment I}
begin
Dir:=-Dir;
Inc(I);
end;
procedure Toggle(var I: integer);
{Toggle Direction and increment I}
begin
Dir:=-Dir;
Inc(I);
end;
procedure Step(var X,Y: integer);
{Take one step "Dir" direction}
begin
X:=X+Dir;
Y:=Y-Dir;
end;
procedure Step(var X,Y: integer);
{Take one step "Dir" direction}
begin
X:=X+Dir;
Y:=Y-Dir;
end;
begin
SetLength(Mat,Size,Size);
Inx:=0; X:=0; Y:=0; Dir:=1;
repeat
begin
Mat[X,Y]:=Inx;
if (X+Dir)>=Size then Toggle(Y)
else if (Y-Dir)>=Size then Toggle(X)
else if (X+Dir)<0 then Toggle(Y)
else if (Y-Dir)<0 then Toggle(X)
else Step(X,Y);
Inc(Inx);
end
begin
Mat[X,Y]:=Inx;
if (X+Dir)>=Size then Toggle(Y)
else if (Y-Dir)>=Size then Toggle(X)
else if (X+Dir)<0 then Toggle(Y)
else if (Y-Dir)<0 then Toggle(X)
else Step(X,Y);
Inc(Inx);
end
until Inx>=Size*Size;
DisplayMatrix(Memo,Mat);
end;

View file

@ -38,7 +38,7 @@ extension op : IntNumber
}
}
public program()
public Program()
{
console.printLine(5.zigzagMatrix()).readChar()
Console.printLine(5.zigzagMatrix()).readChar()
}

View file

@ -3,8 +3,8 @@
-export( [matrix/1, task/0] ).
matrix( N ) ->
{{_X_Y, N}, Proplist} = lists:foldl( fun matrix_as_proplist/2, {{{0, 0}, N}, []}, lists:seq(0, (N * N) - 1) ),
[columns( X, Proplist ) || X <- lists:seq(0, N - 1)].
{{_X_Y, N}, Proplist} = lists:foldl( fun matrix_as_proplist/2, {{{0, 0}, N}, []}, lists:seq(0, (N * N) - 1) ),
[columns( X, Proplist ) || X <- lists:seq(0, N - 1)].
task() -> matrix( 5 ).
@ -13,8 +13,8 @@ task() -> matrix( 5 ).
columns( Column, Proplist ) -> lists:sort( [Value || {{_X, Y}, Value} <- Proplist, Y =:= Column] ).
matrix_as_proplist( N, {{X_Y, Max}, Acc} ) ->
Next = next_indexes( X_Y, Max ),
{{Next, Max}, [{X_Y, N} | Acc]}.
Next = next_indexes( X_Y, Max ),
{{Next, Max}, [{X_Y, N} | Acc]}.
next_indexes( {X, Y}, Max ) when Y + 1 =:= Max, (X + Y) rem 2 =:= 0 -> {X + 1, Y - 1};
next_indexes( {X, Y}, Max ) when Y + 1 =:= Max, (X + Y) rem 2 =:= 1 -> {X + 1, Y};

View file

@ -1,20 +0,0 @@
function zigzag(integer size)
sequence s
integer i, j, d, max
s = repeat(repeat(0,size),size)
i = 1 j = 1 d = -1
max = size*size
for n = 1 to floor(max/2)+1 do
s[i][j] = n
s[size-i+1][size-j+1] = max-n+1
i += d j-= d
if i < 1 then
i += 1 d = -d
elsif j < 1 then
j += 1 d = -d
end if
end for
return s
end function
? zigzag(5)

View file

@ -2,15 +2,15 @@
# Produce a zig-zag array.
# # Variables:
# # Variables:
#
integer DEF_SIZE=5 # Default size = 5
arr_size=${1:-$DEF_SIZE} # $1 = size, or default
integer DEF_SIZE=5 # Default size = 5
arr_size=${1:-$DEF_SIZE} # $1 = size, or default
# # Externals:
# # Externals:
#
# # Functions:
# # Functions:
#
@ -21,12 +21,12 @@ integer i j n
typeset -a zzarr
for (( i=n=0; i<arr_size*2; i++ )); do
for (( j= (i<arr_size) ? 0 : i-arr_size+1; j<=i && j<arr_size; j++ )); do
(( zzarr[(i&1) ? j*(arr_size-1)+i : (i-j)*arr_size+j] = n++ ))
done
for (( j= (i<arr_size) ? 0 : i-arr_size+1; j<=i && j<arr_size; j++ )); do
(( zzarr[(i&1) ? j*(arr_size-1)+i : (i-j)*arr_size+j] = n++ ))
done
done
for ((i=0; i<arr_size*arr_size; i++)); do
printf "%3d " ${zzarr[i]}
(( (i+1)%arr_size == 0 )) && printf "\n"
printf "%3d " ${zzarr[i]}
(( (i+1)%arr_size == 0 )) && printf "\n"
done

View file

@ -1,41 +1,41 @@
Module Lib1 {
Module Global PrintArray(&Ar()) {
if dimension(Ar())<>2 then Error "This is for 2D arrays"
integer i, j, n=dimension(Ar(),1), n1=dimension(Ar(),2)
for i=1 to n
for j=1 to n1
print Ar(i, j),
next
print
next
}
Function Global MakeArray(n as integer=5) {
dim a(1 to n, 1 to n) as integer=0
integer i=1, j=1, z, t1=1
boolean ch=true
for z=0 to n*n-1
if ch then a(i,j)=z else a(j,i)=z
j++
if j>t1 then t1++: j=1:i=t1: ch~ else i--
if i<1 then i=t1 else.if i>n then i=n: j++
if j>n then j=i+2: i=n:ch~
next
=a() // return array (as a pointer)
}
Module Global PrintArray(&Ar()) {
if dimension(Ar())<>2 then Error "This is for 2D arrays"
integer i, j, n=dimension(Ar(),1), n1=dimension(Ar(),2)
for i=1 to n
for j=1 to n1
print Ar(i, j),
next
print
next
}
Function Global MakeArray(n as integer=5) {
dim a(1 to n, 1 to n) as integer=0
integer i=1, j=1, z, t1=1
boolean ch=true
for z=0 to n*n-1
if ch then a(i,j)=z else a(j,i)=z
j++
if j>t1 then t1++: j=1:i=t1: ch~ else i--
if i<1 then i=t1 else.if i>n then i=n: j++
if j>n then j=i+2: i=n:ch~
next
=a() // return array (as a pointer)
}
}
Module Zig_Zag_Matrix (n as integer=5) {
Pen 15 {Report "matrix "+n+"x"+n}
integer old_column=tab
Print $(,4) // set column to 4 chars
if random(1,2)=2 then
dim ret()
ret()=makeArray(n) // this get a copy
else
object a=makeArray(n) // but this get the copy of pointer
link a to ret() // ret() is reference to a, to array
end if
PrintArray &ret()
Print $(,old_column)
Pen 15 {Report "matrix "+n+"x"+n}
integer old_column=tab
Print $(,4) // set column to 4 chars
if random(1,2)=2 then
dim ret()
ret()=makeArray(n) // this get a copy
else
object a=makeArray(n) // but this get the copy of pointer
link a to ret() // ret() is reference to a, to array
end if
PrintArray &ret()
Print $(,old_column)
}
Inline Code Lib1 // just execute the code from module lib1 like was here
Form 60, 36 \\ console 60x36 characters

View file

@ -1,21 +1,21 @@
zz(n)={
my(M=matrix(n,n),i,j,d=-1,start,end=n^2-1);
while(ct--,
M[i+1,j+1]=start;
M[n-i,n-j]=end;
start++;
end--;
i+=d;
j-=d;
if(i<0,
i++;
d=-d
,
if(j<0,
j++;
d=-d
)
);
if(start>end,return(M))
)
my(M=matrix(n,n),i,j,d=-1,start,end=n^2-1);
while(ct--,
M[i+1,j+1]=start;
M[n-i,n-j]=end;
start++;
end--;
i+=d;
j-=d;
if(i<0,
i++;
d=-d
,
if(j<0,
j++;
d=-d
)
);
if(start>end,return(M))
)
};

View file

@ -1,32 +1,32 @@
function ZigZagMatrix($num) {
$matrix = array();
for ($i = 0; $i < $num; $i++){
$matrix[$i] = array();
}
$matrix[$i] = array();
}
$i=1;
$j=1;
$j=1;
for ($e = 0; $e < $num*$num; $e++) {
$matrix[$i-1][$j-1] = $e;
if (($i + $j) % 2 == 0) {
if ($j < $num){
$j++;
}else{
$i += 2;
}
$j++;
}else{
$i += 2;
}
if ($i > 1){
$i --;
}
$i --;
}
} else {
if ($i < $num){
$i++;
}else{
$j += 2;
}
$i++;
}else{
$j += 2;
}
if ($j > 1){
$j --;
}
$j --;
}
}
}
return $matrix;
return $matrix;
}

View file

@ -1,15 +1,15 @@
sub zig_zag {
my ($w, $h, @r, $n) = @_;
$r[ $_->[1] ][ $_->[0] ] = $n++ for
sort { $a->[0] + $a->[1] <=> $b->[0] + $b->[1] or
($a->[0] + $a->[1]) % 2
? $a->[1] <=> $b->[1]
: $a->[0] <=> $b->[0]
}
map { my $e = $_;
map{ [$e, $_] } 0 .. $w-1
} 0 .. $h - 1;
$r[ $_->[1] ][ $_->[0] ] = $n++ for
sort { $a->[0] + $a->[1] <=> $b->[0] + $b->[1] or
($a->[0] + $a->[1]) % 2
? $a->[1] <=> $b->[1]
: $a->[0] <=> $b->[0]
}
map { my $e = $_;
map{ [$e, $_] } 0 .. $w-1
} 0 .. $h - 1;
@r
}

View file

@ -1,22 +1,22 @@
\long\def\antefi#1#2\fi{#2\fi#1}
\def\fornum#1=#2to#3(#4){%
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname
{\number\numexpr#3}{\ifnum\numexpr#4<0 <\else>\fi}{\number\numexpr#4}\noexpand#1}\fornumtemp
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname
{\number\numexpr#3}{\ifnum\numexpr#4<0 <\else>\fi}{\number\numexpr#4}\noexpand#1}\fornumtemp
}
\long\def\fornumi#1#2#3#4#5#6{\def#1{\unless\ifnum#5#3#2\relax\antefi{#6\edef#5{\number\numexpr#5+(#4)\relax}#1}\fi}#1}
\def\elem(#1,#2){\numexpr(#1+#2)*(#1+#2-1)/2-(\ifodd\numexpr#1+#2\relax#1\else#2\fi)\relax}
\def\zzmat#1{%
\noindent% quit vertical mode
\fornum\yy=1to#1(+1){%
\fornum\xx=1to#1(+1){%
\ifnum\numexpr\xx+\yy\relax<\numexpr#1+2\relax
\hbox to 2em{\hfil\number\elem(\xx,\yy)}%
\else
\hbox to 2em{\hfil\number\numexpr#1*#1-1-\elem(#1+1-\xx,#1+1-\yy)\relax}%
\fi
}%
\par\noindent% next line + quit vertical mode
}\par
\noindent% quit vertical mode
\fornum\yy=1to#1(+1){%
\fornum\xx=1to#1(+1){%
\ifnum\numexpr\xx+\yy\relax<\numexpr#1+2\relax
\hbox to 2em{\hfil\number\elem(\xx,\yy)}%
\else
\hbox to 2em{\hfil\number\numexpr#1*#1-1-\elem(#1+1-\xx,#1+1-\yy)\relax}%
\fi
}%
\par\noindent% next line + quit vertical mode
}\par
}
\zzmat{5}
\bye

View file

@ -1,39 +0,0 @@
function zigzag( [int] $n ) {
$zigzag=New-Object 'Object[,]' $n,$n
$nodd = $n -band 1
$nm1 = $n - 1
$i=0;
$j=0;
foreach( $k in 0..( $n * $n - 1 ) ) {
$zigzag[$i,$j] = $k
$iodd = $i -band 1
$jodd = $j -band 1
if( ( $j -eq $nm1 ) -and ( $iodd -ne $nodd ) ) {
$i++
} elseif( ( $i -eq $nm1 ) -and ( $jodd -eq $nodd ) ) {
$j++
} elseif( ( $i -eq 0 ) -and ( -not $jodd ) ) {
$j++
} elseif( ( $j -eq 0 ) -and $iodd ) {
$i++
} elseif( $iodd -eq $jodd ) {
$i--
$j++
} else {
$i++
$j--
}
}
,$zigzag
}
function displayZigZag( [int] $n ) {
$a = zigzag $n
0..$n | ForEach-Object {
$b=$_
$pad=($n*$n-1).ToString().Length
"$(0..$n | ForEach-Object {
"{0,$pad}" -f $a[$b,$_]
} )"
}
}

View file

@ -1 +0,0 @@
zigzag 5 | Format-Wide {"{0,2}" -f $_} -Column 5 -Force

View file

@ -1,31 +1,31 @@
zig_zag(N) :-
zig_zag(N, N).
zig_zag(N, N).
% compute zig_zag for a matrix of Lig lines of Col columns
zig_zag(Lig, Col) :-
length(M, Lig),
maplist(init(Col), M),
fill(M, 0, 0, 0, Lig, Col, up),
% display the matrix
maplist(print_line, M).
length(M, Lig),
maplist(init(Col), M),
fill(M, 0, 0, 0, Lig, Col, up),
% display the matrix
maplist(print_line, M).
fill(M, Cur, L, C, NL, NC, _) :-
L is NL - 1,
C is NC - 1,
nth0(L, M, Line),
nth0(C, Line, Cur).
L is NL - 1,
C is NC - 1,
nth0(L, M, Line),
nth0(C, Line, Cur).
fill(M, Cur, L, C, NL, NC, Sens) :-
nth0(L, M, Line),
nth0(C, Line, Cur),
Cur1 is Cur + 1,
compute_next(NL, NC, L, C, Sens, L1, C1, Sens1),
fill(M, Cur1, L1, C1, NL, NC, Sens1).
nth0(L, M, Line),
nth0(C, Line, Cur),
Cur1 is Cur + 1,
compute_next(NL, NC, L, C, Sens, L1, C1, Sens1),
fill(M, Cur1, L1, C1, NL, NC, Sens1).
init(N, L) :-
length(L, N).
length(L, N).
% compute_next
% arg1 : Number of lnes of the matrix
@ -37,41 +37,41 @@ init(N, L) :-
% arg7 : next column
% arg8 : next direction of movement
compute_next(_NL, NC, 0, Col, up, 0, Col1, down) :-
Col < NC - 1,
Col1 is Col+1.
Col < NC - 1,
Col1 is Col+1.
compute_next(_NL, NC, 0, Col, up, 1, Col, down) :-
Col is NC - 1.
Col is NC - 1.
compute_next(NL, _NC, Lig, 0, down, Lig1, 0, up) :-
Lig < NL - 1,
Lig1 is Lig+1.
Lig < NL - 1,
Lig1 is Lig+1.
compute_next(NL, _NC, Lig, 0, down, Lig, 1, up) :-
Lig is NL - 1.
Lig is NL - 1.
compute_next(NL, _NC, Lig, Col, down, Lig1, Col1, down) :-
Lig < NL - 1,
Lig1 is Lig + 1,
Col1 is Col-1.
Lig < NL - 1,
Lig1 is Lig + 1,
Col1 is Col-1.
compute_next(NL, _NC, Lig, Col, down, Lig, Col1, up) :-
Lig is NL - 1,
Col1 is Col+1.
Lig is NL - 1,
Col1 is Col+1.
compute_next(_NL, NC, Lig, Col, up, Lig1, Col1, up) :-
Col < NC - 1,
Lig1 is Lig - 1,
Col1 is Col+1.
Col < NC - 1,
Lig1 is Lig - 1,
Col1 is Col+1.
compute_next(_NL, NC, Lig, Col, up, Lig1, Col, down) :-
Col is NC - 1,
Lig1 is Lig + 1.
Col is NC - 1,
Lig1 is Lig + 1.
print_line(L) :-
maplist(print_val, L),
nl.
maplist(print_val, L),
nl.
print_val(V) :-
writef('%3r ', [V]).
writef('%3r ', [V]).

View file

@ -1,16 +1,20 @@
COLS = 9
def CX(x, ran):
while True:
x += 2 * next(ran)
yield x
x += 1
yield x
for y in ran:
x += 2 * y
yield x
x += 1
yield x
ran = []
d = -1
for V in CX(1,iter(list(range(0,COLS,2)) + list(range(COLS-1-COLS%2,0,-2)))):
ran.append(iter(range(V, V+COLS*d, d)))
d *= -1
for x in range(0,COLS):
for y in range(x, x+COLS):
print(repr(next(ran[y])).rjust(3), end = ' ')
print()
for V in CX(1, iter(list(range(0, COLS, 2)) + list(range(COLS - 1 - COLS % 2, 0, -2)))):
ran.append(iter(range(V, V + COLS * d, d)))
d *= -1
for x in range(0, COLS):
for y in range(x, x + COLS):
print(repr(next(ran[y])).rjust(3), end=" ")
print()

View file

@ -43,21 +43,21 @@ sub MAIN(Int $size = 5) {
my $t = Turtle.new(dir => 1);
my $counter = 0;
for 1 ..^ $size -> $run {
for ^$run {
$t.lay-egg($counter++);
$t.forward;
}
my $yaw = $run %% 2 ?? -1 !! 1;
$t.turn-right($yaw * 135); $t.forward; $t.turn-right($yaw * 45);
for ^$run {
$t.lay-egg($counter++);
$t.forward;
}
my $yaw = $run %% 2 ?? -1 !! 1;
$t.turn-right($yaw * 135); $t.forward; $t.turn-right($yaw * 45);
}
for $size ... 1 -> $run {
for ^$run -> $ {
$t.lay-egg($counter++);
$t.forward;
}
$t.turn-left(180); $t.forward;
my $yaw = $run %% 2 ?? 1 !! -1;
$t.turn-right($yaw * 45); $t.forward; $t.turn-left($yaw * 45);
for ^$run -> $ {
$t.lay-egg($counter++);
$t.forward;
}
$t.turn-left(180); $t.forward;
my $yaw = $run %% 2 ?? 1 !! -1;
$t.turn-right($yaw * 45); $t.forward; $t.turn-left($yaw * 45);
}
$t.showmap;
}

View file

@ -6,17 +6,17 @@ import IO;
alias cd = tuple[int,int];
public rel[cd, int] zz(int n){
indexorder = sort([<x,y>| x <- [0..n], y <- [0..n]],
bool (cd a, cd b){
if (a[0]+a[1] > b[0]+b[1])
return false;
elseif(a[0] < b[0])
return false;
else
return true;
;
});
return {<indexorder[z] , z> | z <- index(indexorder)};
indexorder = sort([<x,y>| x <- [0..n], y <- [0..n]],
bool (cd a, cd b){
if (a[0]+a[1] > b[0]+b[1])
return false;
elseif(a[0] < b[0])
return false;
else
return true;
;
});
return {<indexorder[z] , z> | z <- index(indexorder)};
}
public void printzz(rel[cd, int] myarray){

View file

@ -7,11 +7,11 @@ fun sign t = if t mod 2 = 0 then ~1 else 1;
fun zag n = List.tabulate (n,
fn i=> rev ( List.tabulate (n,
fn j =>
let val t = n-j+i and u = n+j-i in
if i <= j
let val t = n-j+i and u = n+j-i in
if i <= j
then t*t div 2 + sign t * ( t div 2 - i )
else n*n - 1 - ( u*u div 2 + sign u * ( u div 2 - n + 1 + i) )
end
)));
end
)));
zig zag 5 ;

View file

@ -1,14 +1,14 @@
function zigzag1(n) {
j = 0::n-1
u = J(1, n, (-1, 1))
v = (j:*(2:*j:+3))
v = rowshape((v,v:+1), 1)
a = J(n, n, .)
for (i=1; i<=n; i++) {
a[i, .] = v[j:+i]
v = v+u
}
return(a)
j = 0::n-1
u = J(1, n, (-1, 1))
v = (j:*(2:*j:+3))
v = rowshape((v,v:+1), 1)
a = J(n, n, .)
for (i=1; i<=n; i++) {
a[i, .] = v[j:+i]
v = v+u
}
return(a)
}
zigzag1(5)

View file

@ -1,10 +1,10 @@
function zigzag2(n) {
a = zigzag1(n)
v = (1..n-1):^2
for (i=1; i<n; i++) {
a[n-i+1, i+1..n] = a[n-i+1, i+1..n] - v[1..n-i]
}
return(a)
a = zigzag1(n)
v = (1..n-1):^2
for (i=1; i<n; i++) {
a[n-i+1, i+1..n] = a[n-i+1, i+1..n] - v[1..n-i]
}
return(a)
}
zigzag2(5)

View file

@ -1,7 +0,0 @@
ZigZag ← (
↘₁⇡⊸×₂
⊃(⋅⟜⇡|↧₀-|◿₂|+⊃⍚⇡(\+⊂₀↘₋₁)↧⇌.)
≡↙⊙(≡↻|⍉≡↻|⬚∞≡◇∘⍥⇌)
)
ZigZag 5

View file

@ -1,36 +0,0 @@
ZigZag(Cint(WScript.Arguments(0)))
Function ZigZag(n)
Dim arrZ()
ReDim arrZ(n-1,n-1)
i = 1
j = 1
For e = 0 To (n^2) - 1
arrZ(i-1,j-1) = e
If ((i + j ) And 1) = 0 Then
If j < n Then
j = j + 1
Else
i = i + 2
End If
If i > 1 Then
i = i - 1
End If
Else
If i < n Then
i = i + 1
Else
j = j + 2
End If
If j > 1 Then
j = j - 1
End If
End If
Next
For k = 0 To n-1
For l = 0 To n-1
WScript.StdOut.Write Right(" " & arrZ(k,l),3)
Next
WScript.StdOut.WriteLine
Next
End Function