Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Zig-zag-matrix/ERRE/zig-zag-matrix.erre
Normal file
29
Task/Zig-zag-matrix/ERRE/zig-zag-matrix.erre
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
PROGRAM ZIG_ZAG
|
||||
|
||||
!$DYNAMIC
|
||||
DIM ARRAY%[0,0]
|
||||
|
||||
BEGIN
|
||||
SIZE%=5
|
||||
!$DIM ARRAY%[SIZE%-1,SIZE%-1]
|
||||
|
||||
I%=1
|
||||
J%=1
|
||||
FOR E%=0 TO SIZE%^2-1 DO
|
||||
ARRAY%[I%-1,J%-1]=E%
|
||||
IF ((I%+J%) AND 1)=0 THEN
|
||||
IF J%<SIZE% THEN J%+=1 ELSE I%+=2 END IF
|
||||
IF I%>1 THEN I%-=1 END IF
|
||||
ELSE
|
||||
IF I%<SIZE% THEN I%+=1 ELSE J%+=2 END IF
|
||||
IF J%>1 THEN J%-=1 END IF
|
||||
END IF
|
||||
END FOR
|
||||
|
||||
FOR ROW%=0 TO SIZE%-1 DO
|
||||
FOR COL%=0 TO SIZE%-1 DO
|
||||
WRITE("###";ARRAY%[ROW%,COL%];)
|
||||
END FOR
|
||||
PRINT
|
||||
END FOR
|
||||
END PROGRAM
|
||||
74
Task/Zig-zag-matrix/FreeBASIC/zig-zag-matrix.freebasic
Normal file
74
Task/Zig-zag-matrix/FreeBASIC/zig-zag-matrix.freebasic
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim As Integer n
|
||||
|
||||
Do
|
||||
Input "Enter size of matrix "; n
|
||||
Loop Until n > 0
|
||||
|
||||
Dim zigzag(1 To n, 1 To n) As Integer '' all zero by default
|
||||
|
||||
' enter the numbers 0 to (n^2 - 1) in the matrix's anti-diagonals
|
||||
zigzag(1, 1) = 0
|
||||
If n > 1 Then
|
||||
Dim As Integer row = 0, col = 3
|
||||
Dim As Boolean down = true, increment = true
|
||||
Dim As Integer i = 0, j = 2, k
|
||||
Do
|
||||
If down Then
|
||||
For k = 1 To j
|
||||
i += 1
|
||||
row += 1
|
||||
col -= 1
|
||||
zigzag(row, col) = i
|
||||
Next
|
||||
down = false
|
||||
Else
|
||||
For k = 1 To j
|
||||
i += 1
|
||||
row -= 1
|
||||
col += 1
|
||||
zigzag(row, col) = i
|
||||
Next
|
||||
down = true
|
||||
End If
|
||||
If increment Then
|
||||
j += 1
|
||||
If j > n Then
|
||||
j = n - 1
|
||||
increment = false
|
||||
End If
|
||||
Else
|
||||
j -= 1
|
||||
If j = 0 Then Exit Do
|
||||
End If
|
||||
If down AndAlso increment Then
|
||||
col += 2
|
||||
row -= 1
|
||||
ElseIf Not Down AndAlso increment Then
|
||||
row += 2
|
||||
col -= 1
|
||||
ElseIf down AndAlso Not increment Then
|
||||
col += 1
|
||||
Else '' Not down AndAlso NotIncrement
|
||||
row += 1
|
||||
End If
|
||||
Loop
|
||||
End If
|
||||
|
||||
' print zigzag matrix if n < 20
|
||||
Print
|
||||
If n < 20 Then
|
||||
For i As Integer = 1 To n
|
||||
For j As Integer = 1 To n
|
||||
Print Using "####"; zigzag(i, j);
|
||||
Next j
|
||||
Print
|
||||
Next i
|
||||
Else
|
||||
Print "Matrix is too big to display on 80 column console"
|
||||
End If
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
75
Task/Zig-zag-matrix/Lasso/zig-zag-matrix.lasso
Normal file
75
Task/Zig-zag-matrix/Lasso/zig-zag-matrix.lasso
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
var(
|
||||
'square' = array
|
||||
,'size' = integer( 5 )// for a 5 X 5 square
|
||||
,'row' = array
|
||||
,'x' = integer( 1 )
|
||||
,'y' = integer( 1 )
|
||||
,'counter' = integer( 1 )
|
||||
);
|
||||
|
||||
// create place-holder matrix
|
||||
loop( $size );
|
||||
$row = array;
|
||||
|
||||
loop( $size );
|
||||
$row->insert( 0 );
|
||||
|
||||
/loop;
|
||||
|
||||
$square->insert( $row );
|
||||
|
||||
/loop;
|
||||
|
||||
while( $counter < $size * $size );
|
||||
// check downward diagonal
|
||||
if(
|
||||
$x > 1
|
||||
&&
|
||||
$y < $square->size
|
||||
&&
|
||||
$square->get( $y + 1 )->get( $x - 1 ) == 0
|
||||
);
|
||||
|
||||
$x -= 1;
|
||||
$y += 1;
|
||||
|
||||
// check upward diagonal
|
||||
else(
|
||||
$x < $square->size
|
||||
&&
|
||||
$y > 1
|
||||
&&
|
||||
$square->get( $y - 1 )->get( $x + 1 ) == 0
|
||||
);
|
||||
|
||||
$x += 1;
|
||||
$y -= 1;
|
||||
|
||||
// check right
|
||||
else(
|
||||
(
|
||||
$y == 1
|
||||
||
|
||||
$y == $square->size
|
||||
)
|
||||
&&
|
||||
$x < $square->size
|
||||
&&
|
||||
$square->get( $y )->get( $x + 1 ) == 0
|
||||
);
|
||||
|
||||
$x += 1;
|
||||
|
||||
// down
|
||||
else;
|
||||
$y += 1;
|
||||
|
||||
/if;
|
||||
|
||||
$square->get( $y )->get( $x ) = loop_count;
|
||||
|
||||
$counter += 1;
|
||||
|
||||
/while;
|
||||
|
||||
$square;
|
||||
36
Task/Zig-zag-matrix/Nim/zig-zag-matrix.nim
Normal file
36
Task/Zig-zag-matrix/Nim/zig-zag-matrix.nim
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import algorithm, strutils
|
||||
|
||||
type Pos = tuple[x, y: int]
|
||||
|
||||
template newSeqWith(len: int, init: expr): expr =
|
||||
var result {.gensym.} = newSeq[type(init)](len)
|
||||
for i in 0 .. <len:
|
||||
result[i] = init
|
||||
result
|
||||
|
||||
proc `$`(m: seq[seq[int]]): string =
|
||||
result = ""
|
||||
for r in m:
|
||||
for c in r:
|
||||
result.add align($c, 2) & " "
|
||||
result.add "\n"
|
||||
|
||||
proc zigzagMatrix(n): auto =
|
||||
result = newSeqWith(n, newSeq[int](n))
|
||||
|
||||
var indices = newSeq[Pos]()
|
||||
|
||||
for x in 0 .. <n:
|
||||
for y in 0 .. <n:
|
||||
indices.add((x,y))
|
||||
|
||||
sort(indices, proc(a, b: Pos): int =
|
||||
result = a.x + a.y - b.x - b.y
|
||||
if result == 0: result =
|
||||
(if (a.x + a.y) mod 2 == 0: a.y else: -a.y) -
|
||||
(if (b.x + b.y) mod 2 == 0: b.y else: -b.y))
|
||||
|
||||
for i, p in indices:
|
||||
result[p.x][p.y] = i
|
||||
|
||||
echo zigzagMatrix(6)
|
||||
27
Task/Zig-zag-matrix/Phix/zig-zag-matrix-1.phix
Normal file
27
Task/Zig-zag-matrix/Phix/zig-zag-matrix-1.phix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
integer n = 9
|
||||
integer zstart = 0, zend = n*n-1
|
||||
--integer zstart = 1, zend = n*n
|
||||
string fmt = sprintf("%%%dd",length(sprintf("%d",zend)))
|
||||
sequence m = repeat(repeat("??",n),n)
|
||||
integer x = 1, y = 1, d = -1
|
||||
while 1 do
|
||||
m[x][y] = sprintf(fmt,zstart)
|
||||
if zstart=zend then exit end if
|
||||
zstart += 1
|
||||
m[n-x+1][n-y+1] = sprintf(fmt,zend)
|
||||
zend -= 1
|
||||
x += d
|
||||
y -= d
|
||||
if x<1 then
|
||||
x += 1
|
||||
d = -d
|
||||
elsif y<1 then
|
||||
y += 1
|
||||
d = -d
|
||||
end if
|
||||
end while
|
||||
|
||||
for i=1 to n do
|
||||
m[i] = join(m[i])
|
||||
end for
|
||||
puts(1,join(m,"\n"))
|
||||
17
Task/Zig-zag-matrix/Phix/zig-zag-matrix-2.phix
Normal file
17
Task/Zig-zag-matrix/Phix/zig-zag-matrix-2.phix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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"))
|
||||
24
Task/Zig-zag-matrix/Sidef/zig-zag-matrix.sidef
Normal file
24
Task/Zig-zag-matrix/Sidef/zig-zag-matrix.sidef
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
func zig_zag(w, h) {
|
||||
|
||||
var r = [];
|
||||
var n = 0;
|
||||
|
||||
h.of { |e|
|
||||
w.of { |f|
|
||||
[e-1, f-1]
|
||||
}
|
||||
} \
|
||||
-> reduce('+') \
|
||||
-> sort { |a, b|
|
||||
(a[0]+a[1] <=> b[0]+b[1]) ||
|
||||
(a[0]+a[1] -> is_even ? a[0]<=>b[0]
|
||||
: a[1]<=>b[1])
|
||||
} \
|
||||
-> each { |a|
|
||||
r[a[1]][a[0]] = n++;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
zig_zag(5, 5).each {say .join('', {|i| "%4i" % i})};
|
||||
18
Task/Zig-zag-matrix/jq/zig-zag-matrix-1.jq
Normal file
18
Task/Zig-zag-matrix/jq/zig-zag-matrix-1.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Create an m x n matrix
|
||||
def matrix(m; n; init):
|
||||
if m == 0 then []
|
||||
elif m == 1 then [range(0;n)] | map(init)
|
||||
elif m > 0 then
|
||||
matrix(1;n;init) as $row
|
||||
| [range(0;m)] | map( $row )
|
||||
else error("matrix\(m);_;_) invalid")
|
||||
end ;
|
||||
|
||||
# Print a matrix neatly, each cell occupying n spaces
|
||||
def neatly(n):
|
||||
def right: tostring | ( " " * (n-length) + .);
|
||||
. as $in
|
||||
| length as $length
|
||||
| reduce range (0;$length) as $i
|
||||
(""; . + reduce range(0;$length) as $j
|
||||
(""; "\(.) \($in[$i][$j] | right )" ) + "\n" ) ;
|
||||
19
Task/Zig-zag-matrix/jq/zig-zag-matrix-2.jq
Normal file
19
Task/Zig-zag-matrix/jq/zig-zag-matrix-2.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
def zigzag(n):
|
||||
|
||||
# unless m == n*n, place m at (i,j), pointing
|
||||
# in the direction d, where d = [drow, dcolumn]:
|
||||
def _next(i; j; m; d):
|
||||
if m == (n*n) then . else .[i][j] = m end
|
||||
| if m == (n*n) - 1 then .
|
||||
elif i == n-1 then if j+1 < n then .[i][j+1] = m+1 | _next(i-1; j+2; m+2; [-1, 1]) else . end
|
||||
elif i == 0 then if j+1 < n then .[i][j+1] = m+1 | _next(i+1; j ; m+2; [ 1,-1])
|
||||
else .[i+1][j] = m+1 | _next(i+2; j-1; m+2; [ 1,-1]) end
|
||||
elif j == n-1 then if i+1 < n then .[i+1][j] = m+1 | _next(i+2; j-1; m+2; [ 1,-1]) else . end
|
||||
elif j == 0 then if i+1 < n then .[i+1][j] = m+1 | _next(i; j+1; m+2; [-1, 1])
|
||||
else .[i][j+1] = m+1 | _next(i-1; j+1; m+2; [-1, 1]) end
|
||||
else _next(i+ d[0]; j+ d[1]; m+1; d)
|
||||
end ;
|
||||
matrix(n;n;-1) | _next(0;0; 0; [0,1]) ;
|
||||
|
||||
# Example
|
||||
zigzag(5) | neatly(4)
|
||||
Loading…
Add table
Add a link
Reference in a new issue