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
76
Task/Spiral-matrix/FreeBASIC/spiral-matrix.freebasic
Normal file
76
Task/Spiral-matrix/FreeBASIC/spiral-matrix.freebasic
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Enum Direction
|
||||
across
|
||||
down
|
||||
back
|
||||
up
|
||||
End Enum
|
||||
|
||||
Dim As Integer n
|
||||
|
||||
Do
|
||||
Input "Enter size of matrix "; n
|
||||
Loop Until n > 0
|
||||
|
||||
Dim spiral(1 To n, 1 To n) As Integer '' all zero by default
|
||||
|
||||
' enter the numbers 0 to (n^2 - 1) spirally in the matrix
|
||||
|
||||
Dim As Integer row = 1, col = 1, lowRow = 1, highRow = n, lowCol = 1, highCol = n
|
||||
Dim d As Direction = across
|
||||
|
||||
For i As Integer = 0 To (n * n - 1)
|
||||
spiral(row, col) = i
|
||||
Select Case d
|
||||
Case across
|
||||
col += 1
|
||||
If col > highCol Then
|
||||
col = highCol
|
||||
row += 1
|
||||
d = down
|
||||
End if
|
||||
Case down
|
||||
row += 1
|
||||
If row > highRow Then
|
||||
row = highRow
|
||||
col -= 1
|
||||
d = back
|
||||
End if
|
||||
Case back
|
||||
col -= 1
|
||||
If col < lowCol Then
|
||||
col = lowCol
|
||||
row -= 1
|
||||
d = up
|
||||
lowRow += 1
|
||||
End If
|
||||
Case up
|
||||
row -= 1
|
||||
If row < lowRow Then
|
||||
row = lowRow
|
||||
col += 1
|
||||
d = across
|
||||
highRow -= 1
|
||||
lowCol += 1
|
||||
highCol -= 1
|
||||
End If
|
||||
End Select
|
||||
Next
|
||||
|
||||
' print spiral 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 "####"; spiral(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
|
||||
43
Task/Spiral-matrix/Nim/spiral-matrix.nim
Normal file
43
Task/Spiral-matrix/Nim/spiral-matrix.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import strutils
|
||||
|
||||
type Pos = tuple[x, y: int]
|
||||
|
||||
proc newSeqWith[T](len: int, init: T): seq[T] =
|
||||
result = newSeq[T] len
|
||||
for i in 0 .. <len:
|
||||
result[i] = init
|
||||
|
||||
proc `^`*(base: int, exp: int): int =
|
||||
var (base, exp) = (base, exp)
|
||||
result = 1
|
||||
|
||||
while exp != 0:
|
||||
if (exp and 1) != 0:
|
||||
result *= base
|
||||
exp = exp shr 1
|
||||
base *= base
|
||||
|
||||
proc `$`(m: seq[seq[int]]): string =
|
||||
result = ""
|
||||
for r in m:
|
||||
for c in r:
|
||||
result.add align($c, 2) & " "
|
||||
result.add "\n"
|
||||
|
||||
proc spiral(n): auto =
|
||||
result = newSeqWith(n, newSeqWith[int](n, -1))
|
||||
var dx = 1
|
||||
var dy, x, y = 0
|
||||
for i in 0 .. <(n^2):
|
||||
result[y][x] = i
|
||||
let (nx, ny) = (x+dx, y+dy)
|
||||
if nx in 0 .. <n and ny in 0 .. <n and result[ny][nx] == -1:
|
||||
x = nx
|
||||
y = ny
|
||||
else:
|
||||
swap dx, dy
|
||||
dx = -dx
|
||||
x = x + dx
|
||||
y = y + dy
|
||||
|
||||
echo spiral(5)
|
||||
19
Task/Spiral-matrix/Phix/spiral-matrix.phix
Normal file
19
Task/Spiral-matrix/Phix/spiral-matrix.phix
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
integer n = 5
|
||||
string fmt = sprintf("%%%dd",length(sprintf("%d",n*n)))
|
||||
integer x = 1, y = 0, c = 0, dx = 0, dy = 1, len = n
|
||||
sequence m = repeat(repeat("??",n),n)
|
||||
for i=1 to 2*n do -- 2n runs..
|
||||
for j=1 to len do -- of a length...
|
||||
x += dx
|
||||
y += dy
|
||||
m[x][y] = sprintf(fmt,c)
|
||||
c += 1
|
||||
end for
|
||||
len -= and_bits(i,1) -- ..-1 every other
|
||||
{dx,dy} = {dy,-dx} -- in new direction
|
||||
end for
|
||||
|
||||
for i=1 to n do
|
||||
m[i] = join(m[i])
|
||||
end for
|
||||
puts(1,join(m,"\n"))
|
||||
20
Task/Spiral-matrix/Sidef/spiral-matrix.sidef
Normal file
20
Task/Spiral-matrix/Sidef/spiral-matrix.sidef
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
func spiral(n) {
|
||||
var (x, y, dx, dy, a) = (0, 0, 1, 0, []);
|
||||
{ |i|
|
||||
a[y][x] = i;
|
||||
var (nx, ny) = (x+dx, y+dy);
|
||||
( if (dx == 1 && (nx == n || a[ny][nx]!=nil)) { [ 0, 1] }
|
||||
elsif (dy == 1 && (ny == n || a[ny][nx]!=nil)) { [-1, 0] }
|
||||
elsif (dx == -1 && (nx < 0 || a[ny][nx]!=nil)) { [ 0, -1] }
|
||||
elsif (dy == -1 && (ny < 0 || a[ny][nx]!=nil)) { [ 1, 0] }
|
||||
else { [dx, dy] }
|
||||
) » (\dx, \dy);
|
||||
x = x+dx;
|
||||
y = y+dy;
|
||||
} * n**2;
|
||||
return a;
|
||||
}
|
||||
|
||||
spiral(5).each { |row|
|
||||
row.map {"%3d" % _}.join(' ').say;
|
||||
}
|
||||
26
Task/Spiral-matrix/jq/spiral-matrix-1.jq
Normal file
26
Task/Spiral-matrix/jq/spiral-matrix-1.jq
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# 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" ) ;
|
||||
|
||||
def right:
|
||||
if . == [1, 0] then [ 0, -1]
|
||||
elif . == [0, -1] then [-1, 0]
|
||||
elif . == [-1, 0] then [ 0, 1]
|
||||
elif . == [0, 1] then [ 1, 0]
|
||||
else error("invalid direction: \(.)")
|
||||
end;
|
||||
14
Task/Spiral-matrix/jq/spiral-matrix-2.jq
Normal file
14
Task/Spiral-matrix/jq/spiral-matrix-2.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def spiral(n):
|
||||
# we just placed m at i,j, and we are moving in the direction d
|
||||
def _next(i; j; m; d):
|
||||
if m == (n*n) - 1 then .
|
||||
elif .[i+d[0]][j+d[1]] == false
|
||||
then .[i+d[0]][j+d[1]] = m+1 | _next(i+d[0]; j+d[1]; m+1; d)
|
||||
else (d|right) as $d
|
||||
| .[i+$d[0]][j+$d[1]] = m+1 | _next(i+$d[0]; j+$d[1]; m+1; $d)
|
||||
end;
|
||||
|
||||
matrix(n;n;false) | .[0][0] = 0 | _next(0;0;0; [0,1]) ;
|
||||
|
||||
# Example
|
||||
spiral(5) | neatly(3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue