Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Spiral-matrix/Nim/spiral-matrix.nim
Normal file
27
Task/Spiral-matrix/Nim/spiral-matrix.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import sequtils, strutils
|
||||
|
||||
proc `$`(m: seq[seq[int]]): string =
|
||||
for r in m:
|
||||
let lg = result.len
|
||||
for c in r:
|
||||
result.addSep(" ", lg)
|
||||
result.add align($c, 2)
|
||||
result.add '\n'
|
||||
|
||||
proc spiral(n: Positive): seq[seq[int]] =
|
||||
result = newSeqWith(n, repeat(-1, n))
|
||||
var dx = 1
|
||||
var dy, x, y = 0
|
||||
for i in 0 ..< (n * n):
|
||||
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 += dx
|
||||
y += dy
|
||||
|
||||
echo spiral(5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue