Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Spiral-matrix/Scala/spiral-matrix.scala
Normal file
22
Task/Spiral-matrix/Scala/spiral-matrix.scala
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Folder(){
|
||||
var dir = (1,0)
|
||||
var pos = (-1,0)
|
||||
def apply(l:List[Int], a:Array[Array[Int]]) = {
|
||||
var (x,y) = pos //start position
|
||||
var (dx,dy) = dir //direction
|
||||
l.foreach {e => x = x + dx; y = y + dy; a(y)(x) = e } //copy l elements to array using current direction
|
||||
pos = (x,y)
|
||||
dir = (-dy, dx) //turn
|
||||
}
|
||||
}
|
||||
def spiral(n:Int) = {
|
||||
def dup(n:Int) = (1 to n).flatMap(i=>List(i,i)).toList
|
||||
val folds = n :: dup(n-1).reverse //define fold part lengths
|
||||
|
||||
var array = new Array[Array[Int]](n,n)
|
||||
val fold = new Folder()
|
||||
|
||||
var seq = (0 until n*n).toList //sequence to fold
|
||||
folds.foreach {len => fold(seq.take(len),array); seq = seq.drop(len)}
|
||||
array
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue