June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,27 +1,21 @@
|
|||
function rs = runsum(v)
|
||||
for i = 1:numel(v)
|
||||
rs(i) = sum(v(1:i));
|
||||
function a = spiral(n)
|
||||
a = ones(n*n, 1);
|
||||
u = -(i = n) * (v = ones(n, 1));
|
||||
for k = n-1:-1:1
|
||||
j = 1:k;
|
||||
a(j+i) = u(j) = -u(j);
|
||||
a(j+(i+k)) = v(j) = -v(j);
|
||||
i += 2*k;
|
||||
endfor
|
||||
a(cumsum(a)) = 1:n*n;
|
||||
a = reshape(a, n, n)'-1;
|
||||
endfunction
|
||||
|
||||
function g = grade(v)
|
||||
for i = 1:numel(v)
|
||||
g(v(i)+1) = i-1;
|
||||
endfor
|
||||
endfunction
|
||||
>> spiral(5)
|
||||
ans =
|
||||
|
||||
function spiral = make_spiral(spirald)
|
||||
series = ones(1,spirald^2);
|
||||
l = spirald-1; p = spirald+1;
|
||||
s = 1;
|
||||
while(l>0)
|
||||
series(p:p+l-1) *= spirald*s;
|
||||
series(p+l:p+l*2-1) *= -s;
|
||||
p += l*2;
|
||||
l--; s *= -1;
|
||||
endwhile
|
||||
series(1) = 0;
|
||||
spiral = reshape(grade(runsum(series)), spirald, spirald)';
|
||||
endfunction
|
||||
|
||||
make_spiral(5)
|
||||
0 1 2 3 4
|
||||
15 16 17 18 5
|
||||
14 23 24 19 6
|
||||
13 22 21 20 7
|
||||
12 11 10 9 8
|
||||
|
|
|
|||
|
|
@ -38,3 +38,22 @@ class Turtle {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Now we can build the spiral in the normal way from outside-in like this:
|
||||
|
||||
sub MAIN(Int $size = 5) {
|
||||
my $t = Turtle.new(dir => 2);
|
||||
my $counter = 0;
|
||||
$t.forward(-1);
|
||||
for 0..^ $size -> $ {
|
||||
$t.forward;
|
||||
$t.lay-egg($counter++);
|
||||
}
|
||||
for $size-1 ... 1 -> $run {
|
||||
$t.turn-right;
|
||||
$t.forward, $t.lay-egg($counter++) for 0..^$run;
|
||||
$t.turn-right;
|
||||
$t.forward, $t.lay-egg($counter++) for 0..^$run;
|
||||
}
|
||||
$t.showmap;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,11 @@
|
|||
sub MAIN($size as Int) {
|
||||
my $t = Turtle.new(dir => 2);
|
||||
my $counter = 0;
|
||||
$t.forward(-1);
|
||||
for 0..^ $size -> $ {
|
||||
sub MAIN(Int $size = 5) {
|
||||
my $t = Turtle.new(dir => ($size %% 2 ?? 4 !! 0));
|
||||
my $counter = $size * $size;
|
||||
while $counter {
|
||||
$t.lay-egg(--$counter);
|
||||
$t.turn-left;
|
||||
$t.turn-right if $t.look;
|
||||
$t.forward;
|
||||
$t.lay-egg($counter++);
|
||||
}
|
||||
for $size-1 ... 1 -> $run {
|
||||
$t.turn-right;
|
||||
$t.forward, $t.lay-egg($counter++) for 0..^$run;
|
||||
$t.turn-right;
|
||||
$t.forward, $t.lay-egg($counter++) for 0..^$run;
|
||||
}
|
||||
$t.showmap;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,19 @@
|
|||
sub MAIN($size as Int) {
|
||||
my $t = Turtle.new(dir => ($size %% 2 ?? 4 !! 0));
|
||||
my $counter = $size * $size;
|
||||
while $counter {
|
||||
$t.lay-egg(--$counter);
|
||||
$t.turn-left;
|
||||
$t.turn-right if $t.look;
|
||||
$t.forward;
|
||||
}
|
||||
$t.showmap;
|
||||
sub spiral_matrix ( $n ) {
|
||||
my @sm;
|
||||
my $len = $n;
|
||||
my $pos = 0;
|
||||
|
||||
for ^($n/2).ceiling -> $i {
|
||||
my $j = $i + 1;
|
||||
my $e = $n - $j;
|
||||
|
||||
@sm[$i ][$i + $_] = $pos++ for ^( $len); # Top
|
||||
@sm[$j + $_][$e ] = $pos++ for ^(--$len); # Right
|
||||
@sm[$e ][$i + $_] = $pos++ for reverse ^( $len); # Bottom
|
||||
@sm[$j + $_][$i ] = $pos++ for reverse ^(--$len); # Left
|
||||
}
|
||||
|
||||
return @sm;
|
||||
}
|
||||
|
||||
say .fmt('%3d') for spiral_matrix(5);
|
||||
|
|
|
|||
51
Task/Spiral-matrix/Prolog/spiral-matrix.pro
Normal file
51
Task/Spiral-matrix/Prolog/spiral-matrix.pro
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
% Prolog implementation: SWI-Prolog 7.2.3
|
||||
|
||||
replace([_|T], 0, E, [E|T]) :- !.
|
||||
replace([H|T], N, E, Xs) :-
|
||||
succ(N1, N), replace(T, N1, E, Xs1), Xs = [H|Xs1].
|
||||
|
||||
% True if Xs is the Original grid with the element at (X, Y) replaces by E.
|
||||
replace_in([H|T], (0, Y), E, Xs) :- replace(H, Y, E, NH), Xs = [NH|T], !.
|
||||
replace_in([H|T], (X, Y), E, Xs) :-
|
||||
succ(X1, X), replace_in(T, (X1, Y), E, Xs1), Xs = [H|Xs1].
|
||||
|
||||
% True, if E is the value at (X, Y) in Xs
|
||||
get_in(Xs, (X, Y), E) :- nth0(X, Xs, L), nth0(Y, L, E).
|
||||
|
||||
create(N, Mx) :- % NxN grid full of nils
|
||||
numlist(1, N, Ns),
|
||||
findall(X, (member(_, Ns), X = nil), Ls),
|
||||
findall(X, (member(_, Ns), X = Ls), Mx).
|
||||
|
||||
% Depending of the direction, returns two possible coordinates and directions
|
||||
% (C,D) that will be used in case of a turn, and (A,B) otherwise.
|
||||
ops(right, (X,Y), (A,B), (C,D), D1, D2) :-
|
||||
A is X, B is Y+1, D1 = right, C is X+1, D is Y, D2 = down.
|
||||
|
||||
ops(left, (X,Y), (A,B), (C,D), D1, D2) :-
|
||||
A is X, B is Y-1, D1 = left, C is X-1, D is Y, D2 = up.
|
||||
|
||||
ops(up, (X,Y), (A,B), (C,D), D1, D2) :-
|
||||
A is X-1, B is Y, D1 = up, C is X, D is Y+1, D2 = right.
|
||||
|
||||
ops(down, (X,Y), (A,B), (C,D), D1, D2) :-
|
||||
A is X+1, B is Y, D1 = down, C is X, D is Y-1, D2 = left.
|
||||
|
||||
% True if NCoor is the right coor in spiral shape. Returns a new direction also.
|
||||
next(Dir, Mx, Coor, NCoor, NDir) :-
|
||||
ops(Dir, Coor, C1, C2, D1, D2),
|
||||
(get_in(Mx, C1, nil) -> NCoor = C1, NDir = D1
|
||||
; NCoor = C2, NDir = D2).
|
||||
|
||||
% Returns an spiral with [H|Vs] elements called R, only work if the length of
|
||||
% [H|Vs], is the square of the size of the grid.
|
||||
spiralH(Dir, Mx, Coor, [H|Vs], R) :-
|
||||
replace_in(Mx, Coor, H, NMx),
|
||||
(Vs = [] -> R = NMx
|
||||
; next(Dir, Mx, Coor, NCoor, NDir),
|
||||
spiralH(NDir, NMx, NCoor, Vs, R)).
|
||||
|
||||
% True if Mx is the grid in spiral shape of the numbers from 0 to N*N-1.
|
||||
spiral(N, Mx) :-
|
||||
Sq is N*N-1, numlist(0, Sq, Ns),
|
||||
create(N, EMx), spiralH(right, EMx, (0,0), Ns, Mx).
|
||||
46
Task/Spiral-matrix/Ring/spiral-matrix.ring
Normal file
46
Task/Spiral-matrix/Ring/spiral-matrix.ring
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# Project : Spiral matrix
|
||||
# Date : 2018/03/23
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
load "stdlib.ring"
|
||||
n = 5
|
||||
result = newlist(n,n)
|
||||
k = 1
|
||||
top = 1
|
||||
bottom = n
|
||||
left = 1
|
||||
right = n
|
||||
while (k<=n*n)
|
||||
for i=left to right
|
||||
result[top][i]=k
|
||||
k = k + 1
|
||||
next
|
||||
top = top + 1
|
||||
for i=top to bottom
|
||||
result[i][right]=k
|
||||
k = k + 1
|
||||
next
|
||||
right = right - 1
|
||||
for i=right to left step -1
|
||||
result[bottom][i]=k
|
||||
k = k + 1
|
||||
next
|
||||
bottom = bottom - 1
|
||||
for i=bottom to top step -1
|
||||
result[i][left] = k
|
||||
k = k + 1
|
||||
next
|
||||
left = left + 1
|
||||
end
|
||||
for m = 1 to n
|
||||
for p = 1 to n
|
||||
if m = 1
|
||||
see " " + result[m][p]
|
||||
else
|
||||
see "" + result[m][p] + " "
|
||||
ok
|
||||
next
|
||||
see nl
|
||||
next
|
||||
see nl
|
||||
|
|
@ -1,22 +1,25 @@
|
|||
// Spiral Matrix
|
||||
n=10
|
||||
mat=zeros(n,n);
|
||||
botcol=1; topcol=n
|
||||
botrow=1; toprow=n
|
||||
ndir=0; col=1; row=1;
|
||||
for i=0:n*n-1
|
||||
mat(row,col)=i;
|
||||
if ndir==0
|
||||
if col<topcol then col=col+1; else ndir=1, row=row+1; botrow=botrow+1; end
|
||||
elseif ndir==1
|
||||
if row<toprow then row=row+1; else ndir=2, col=col-1; topcol=topcol-1; end
|
||||
elseif ndir==2
|
||||
if col>botcol then col=col-1; else ndir=3, row=row-1; toprow=toprow-1; end
|
||||
elseif ndir==3
|
||||
if row>botrow then row=row-1; else ndir=0, col=col+1; botcol=botcol+1; end
|
||||
end
|
||||
end i
|
||||
printf("n=%4d\n",n);
|
||||
for i=1:n;
|
||||
for j=1:n; printf("%4d",mat(i,j)); end j; printf("\n");
|
||||
end i;
|
||||
function a = spiral(n)
|
||||
a = ones(n*n, 1)
|
||||
v = ones(n, 1)
|
||||
u = -n*v;
|
||||
i = n
|
||||
for k = n-1:-1:1
|
||||
j = 1:k
|
||||
u(j) = -u(j)
|
||||
a(j+i) = u(j)
|
||||
v(j) = -v(j)
|
||||
a(j+(i+k)) = v(j)
|
||||
i = i+2*k
|
||||
end
|
||||
a(cumsum(a)) = (1:n*n)'
|
||||
a = matrix(a, n, n)'-1
|
||||
endfunction
|
||||
|
||||
-->spiral(5)
|
||||
ans =
|
||||
|
||||
0. 1. 2. 3. 4.
|
||||
15. 16. 17. 18. 5.
|
||||
14. 23. 24. 19. 6.
|
||||
13. 22. 21. 20. 7.
|
||||
12. 11. 10. 9. 8.
|
||||
|
|
|
|||
21
Task/Spiral-matrix/Stata/spiral-matrix.stata
Normal file
21
Task/Spiral-matrix/Stata/spiral-matrix.stata
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
function spiral_mat(n) {
|
||||
a = J(n*n, 1, 1)
|
||||
u = J(n, 1, -n)
|
||||
v = J(n, 1, 1)
|
||||
for (k=(i=n)-1; k>=1; i=i+2*k--) {
|
||||
j = 1..k
|
||||
a[j:+i] = u[j] = -u[j]
|
||||
a[j:+(i+k)] = v[j] = -v[j]
|
||||
}
|
||||
return(rowshape(invorder(runningsum(a)),n):-1)
|
||||
}
|
||||
|
||||
spiral_mat(5)
|
||||
1 2 3 4 5
|
||||
+--------------------------+
|
||||
1 | 0 1 2 3 4 |
|
||||
2 | 15 16 17 18 5 |
|
||||
3 | 14 23 24 19 6 |
|
||||
4 | 13 22 21 20 7 |
|
||||
5 | 12 11 10 9 8 |
|
||||
+--------------------------+
|
||||
Loading…
Add table
Add a link
Reference in a new issue