June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,28 @@
function a = zigzag1(n)
j = 1:n;
u = repmat([-1; 1], n, 1);
v = j.*(2*j-3);
v = reshape([v; v+1], 2*n, 1);
a = zeros(n, n);
for i = 1:n
a(:, i) = v(i+j);
v += u;
endfor
endfunction
function a = zigzag2(n)
a = zigzag1(n);
v = (1:n-1)'.^2;
for i = 2:n
a(n+2-i:n, i) -= v(1:i-1);
endfor
endfunction
>> zigzag2(5)
ans =
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

View file

@ -0,0 +1,18 @@
function a = zigzag3(n)
a = zeros(n, n);
for k=1:n
d = (2*(j = mod(k, 2))-1)*(n-1);
m = (n-1)*(k-1);
a(k+(1-j)*m:d:k+j*m) = k*(k-1)/2:k*(k+1)/2-1;
a(n*(n+1-k)+(1-j)*m:d:n*(n+1-k)+j*m) = n*n-k*(k+1)/2:n*n-k*(k-1)/2-1;
endfor
endfunction
>> zigzag3(5)
ans =
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

View file

@ -1,4 +1,45 @@
sub MAIN($size as Int) {
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 {

View file

@ -0,0 +1,56 @@
# Project : Zig-zag matrix
# Date : 2018/03/24
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
n = 5
a = newlist(n,n)
for j = 1 to n
for i = 1 to n
a[j][i] = 0
next
next
i = 1
j = 1
k = 1
while k < n * n
a[j][i] = k
k = k + 1
if i = n
j = j + 1
a[j][i] = k
k = k + 1
di = -1
dj = 1
ok
if j = 1
i = i + 1
a[j][i] = k
k = k + 1
di = -1
dj = 1
ok
if j = n
i = i + 1
a[j][i] = k
k = k + 1
di = 1
dj = -1
ok
if i = 1
j = j + 1
a[j][i] = k
k = k + 1
di = 1
dj = -1
ok
i = i + di
j = j + dj
end
for p = 1 to n
for m = 1 to n
see "" + a[p][m] + " "
next
see nl
next

View file

@ -0,0 +1,19 @@
function a = zigzag3(n)
a = zeros(n, n)
for k=1:n
j = modulo(k, 2)
d = (2*j-1)*(n-1)
m = (n-1)*(k-1)
a(k+(1-j)*m:d:k+j*m) = k*(k-1)/2:k*(k+1)/2-1
a(n*(n+1-k)+(1-j)*m:d:n*(n+1-k)+j*m) = n*n-k*(k+1)/2:n*n-k*(k-1)/2-1
end
endfunction
-->zigzag3(5)
ans =
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.

View file

@ -0,0 +1,22 @@
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)
}
zigzag1(5)
1 2 3 4 5
+--------------------------+
1 | 0 1 5 6 14 |
2 | 2 4 7 13 16 |
3 | 3 8 12 17 25 |
4 | 9 11 18 24 31 |
5 | 10 19 23 32 40 |
+--------------------------+

View file

@ -0,0 +1,18 @@
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)
}
zigzag2(5)
1 2 3 4 5
+--------------------------+
1 | 0 1 5 6 14 |
2 | 2 4 7 13 15 |
3 | 3 8 12 16 21 |
4 | 9 11 17 20 22 |
5 | 10 18 19 23 24 |
+--------------------------+

View file

@ -0,0 +1,10 @@
zigzag1(5)-zigzag2(5)
[symmetric]
1 2 3 4 5
+--------------------------+
1 | 0 |
2 | 0 0 |
3 | 0 0 0 |
4 | 0 0 1 4 |
5 | 0 1 4 9 16 |
+--------------------------+