Zig-zag matrix: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 954:
20 22 32 35 41 44 46
21 33 34 42 43 47 48</pre>
 
=={{header|C sharp}}==
 
<lang csharp>public static int[,] ZigZag(int n)
{
int[,] result = new int[n, n];
int i = 0, j = 0;
int d = -1; // -1 for top-right move, +1 for bottom-left move
int start = 0, end = n * n - 1;
do
{
result[i, j] = start++;
result[n - i - 1, n - j - 1] = end--;
 
i += d; j -= d;
if (i < 0)
{
i++; d = -d; // top reached, reverse
}
else if (j < 0)
{
j++; d = -d; // left reached, reverse
}
} while (start < end);
if (start == end)
result[i, j] = start;
return result;
}</lang>
 
=={{header|C++}}==
Line 1,045 ⟶ 1,073:
10 18 19 23 24
</pre>
 
=={{header|C sharp}}==
 
<lang csharp>public static int[,] ZigZag(int n)
{
int[,] result = new int[n, n];
int i = 0, j = 0;
int d = -1; // -1 for top-right move, +1 for bottom-left move
int start = 0, end = n * n - 1;
do
{
result[i, j] = start++;
result[n - i - 1, n - j - 1] = end--;
 
i += d; j -= d;
if (i < 0)
{
i++; d = -d; // top reached, reverse
}
else if (j < 0)
{
j++; d = -d; // left reached, reverse
}
} while (start < end);
if (start == end)
result[i, j] = start;
return result;
}</lang>
 
=={{header|Ceylon}}==
Line 1,233:
[ 20, 21, 29, 30, 34, 35 ] ]
</pre>
 
 
=={{header|Common Lisp}}==
Line 1,527 ⟶ 1,526:
[10,18,19,23,24]]
</pre>
 
 
=={{header|ERRE}}==
Line 1,846 ⟶ 1,844:
9 11 17 20 22
10 18 19 23 24 ok</lang>
 
 
=={{header|Fortran}}==
Line 2,016 ⟶ 2,013:
# [ 9, 11, 17, 20, 22 ],
# [ 10, 18, 19, 23, 24 ] ]</lang>
 
=={{header|Go}}==
{{trans|Groovy}} Edge direct algorithm
Line 3,367 ⟶ 3,365:
9 11 17 20 22
10 18 19 23 24</pre>
 
 
=={{header|Maxima}}==
Line 3,638 ⟶ 3,635:
}
</lang>
 
 
=={{header|OCaml}}==
Line 4,109 ⟶ 4,105:
 
print map{ "@$_\n" } zig_zag(3, 5);</lang>
 
=={{header|Perl 6}}==
Using the same Turtle class as in the [[Spiral_matrix#Perl_6|Spiral matrix]] task:
 
<lang perl6>class Turtle {
my @dv = [0,-1], [1,-1], [1,0], [1,1], [0,1], [-1,1], [-1,0], [-1,-1];
my $points = 8; # 'compass' points of neighbors on grid: north=0, northeast=1, east=2, etc.
has @.loc = 0,0;
has $.dir = 0;
has %.world;
has $.maxegg;
has $.range-x;
has $.range-y;
method turn-left ($angle = 90) { $!dir -= $angle / 45; $!dir %= $points; }
method turn-right($angle = 90) { $!dir += $angle / 45; $!dir %= $points; }
method lay-egg($egg) {
%!world{~@!loc} = $egg;
$!maxegg max= $egg;
$!range-x minmax= @!loc[0];
$!range-y minmax= @!loc[1];
}
method look($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
%!world{~$there};
}
method forward($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
@!loc = @($there);
}
method showmap() {
my $form = "%{$!maxegg.chars}s";
my $endx = $!range-x.max;
for $!range-y.list X $!range-x.list -> ($y, $x) {
print (%!world{"$x $y"} // '').fmt($form);
print $x == $endx ?? "\n" !! ' ';
}
}
}
 
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 $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);
}
$t.showmap;
}</lang>
 
=={{header|Phix}}==
Line 4,373 ⟶ 4,302:
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24 </pre>
 
=={{header|PlainTeX}}==
Line 4,911 ⟶ 4,840:
10 18 19 23 24
</pre>
 
=={{header|Rascal}}==
{{incorrect|Rascal|Output is striped rather than zig-zag
i.e. your numbers always increase going diagonally down and to the left when it should alternativly increase/decrease.}}
This is a translation of the [[Zig-zag_matrix#Python|Python]] example.
As explained on the [[Talk:Zig-zag_matrix#anti-diagonals|Talk]] page,
the key way to understand a zig-zag matrix is to write down
an example with coordinates:
<lang rascal>0 (0,0), 1 (0,1), 3 (0,2)
2 (1,0), 4 (1,1), 6 (1,2)
5 (2,0), 7 (2,1), 8 (2,2)</lang>
If you order these coordinates on the number, you create the order:
<lang rascal> 0 (0,0), 1 (0,1), 2 (1,0), 3 (0,2), 4 (1,1), 5 (2,0), 6 (1,2), 7 (2,1), 8 (2,2)</lang>
One can observe that this increases with the sum of the coordinates,
and secondly with the the first number of the coordinates.
The Rascal example uses this phenomenon:
<lang rascal>import util::Math;
import List;
import Set;
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)};
}
 
public void printzz(rel[cd, int] myarray){
n = floor(sqrt(size(myarray)));
for (x <- [0..n-1]){
for (y <- [0..n-1]){
print("<myarray[<y,x>]>\t");}
println();}
}</lang>
{{out}}
<lang rascal>rascal>printzz(zz(4))
{0} {1} {3} {6} {10}
{2} {4} {7} {11} {15}
{5} {8} {12} {16} {19}
{9} {13} {17} {20} {22}
{14} {18} {21} {23} {24}
ok</lang>
 
=={{header|Qi}}==
Line 5,056 ⟶ 4,933:
(6 12 13 15))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Using the same Turtle class as in the [[Spiral_matrix#Perl_6|Spiral matrix]] task:
 
<lang perl6>class Turtle {
my @dv = [0,-1], [1,-1], [1,0], [1,1], [0,1], [-1,1], [-1,0], [-1,-1];
my $points = 8; # 'compass' points of neighbors on grid: north=0, northeast=1, east=2, etc.
has @.loc = 0,0;
has $.dir = 0;
has %.world;
has $.maxegg;
has $.range-x;
has $.range-y;
method turn-left ($angle = 90) { $!dir -= $angle / 45; $!dir %= $points; }
method turn-right($angle = 90) { $!dir += $angle / 45; $!dir %= $points; }
method lay-egg($egg) {
%!world{~@!loc} = $egg;
$!maxegg max= $egg;
$!range-x minmax= @!loc[0];
$!range-y minmax= @!loc[1];
}
method look($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
%!world{~$there};
}
method forward($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
@!loc = @($there);
}
method showmap() {
my $form = "%{$!maxegg.chars}s";
my $endx = $!range-x.max;
for $!range-y.list X $!range-x.list -> ($y, $x) {
print (%!world{"$x $y"} // '').fmt($form);
print $x == $endx ?? "\n" !! ' ';
}
}
}
 
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 $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);
}
$t.showmap;
}</lang>
 
=={{header|Rascal}}==
{{incorrect|Rascal|Output is striped rather than zig-zag
i.e. your numbers always increase going diagonally down and to the left when it should alternativly increase/decrease.}}
This is a translation of the [[Zig-zag_matrix#Python|Python]] example.
As explained on the [[Talk:Zig-zag_matrix#anti-diagonals|Talk]] page,
the key way to understand a zig-zag matrix is to write down
an example with coordinates:
<lang rascal>0 (0,0), 1 (0,1), 3 (0,2)
2 (1,0), 4 (1,1), 6 (1,2)
5 (2,0), 7 (2,1), 8 (2,2)</lang>
If you order these coordinates on the number, you create the order:
<lang rascal> 0 (0,0), 1 (0,1), 2 (1,0), 3 (0,2), 4 (1,1), 5 (2,0), 6 (1,2), 7 (2,1), 8 (2,2)</lang>
One can observe that this increases with the sum of the coordinates,
and secondly with the the first number of the coordinates.
The Rascal example uses this phenomenon:
<lang rascal>import util::Math;
import List;
import Set;
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)};
}
 
public void printzz(rel[cd, int] myarray){
n = floor(sqrt(size(myarray)));
for (x <- [0..n-1]){
for (y <- [0..n-1]){
print("<myarray[<y,x>]>\t");}
println();}
}</lang>
{{out}}
<lang rascal>rascal>printzz(zz(4))
{0} {1} {3} {6} {10}
{2} {4} {7} {11} {15}
{5} {8} {12} {16} {19}
{9} {13} {17} {20} {22}
{14} {18} {21} {23} {24}
ok</lang>
 
=={{header|REXX}}==
10,327

edits