Zig-zag matrix: Difference between revisions

m (→‎{{header|REXX}}: added a comment to the REXX section header.)
Line 4,564:
Uses the array indices sort solution used by others here.
 
<lang scala>def zigzag(n:int Int): Array[Array[Int]] = {
varval l = ListListBuffer[Tuple2[int(Int,int] Int)]()
(0 until n*n) foreach {i =>l = l += ((i%n, i/n))}
val lSorted = l.sortWith {
l = l.sort{case ((x,y), (u,v)) => if (x+y == u+v)
if ((x+y) % 2 == 0) x<u else y<v
elseif (x+y) <== (u+v) }
if ((x+y) % 2 == 0) x<u else y<v
var a = new Array[Array[int]](n,n)
l.zipWithIndex foreach {case ( else (x,+y),i) =>< a(yu+v)(x) = i}
}
varval a = new Array.ofDim[Array[int]Int](n, n)
lSorted.zipWithIndex foreach {
sort{ case ((x,y),(u,v) i) => if a(y)(x+y) == u+v) i
}
a
}</lang>
 
Or, compressed into just one statement
 
<lang scala>def zigzag(n:int) = {
var indices = List[Tuple2[Int,Int]]()
var array = new Array[Array[Int]](n,n)
 
(0 until n*n).foldLeft(indices)((l,i) => l + (i%n,i/n)).
sort{case ((x,y),(u,v)) => if (x+y == u+v)
if ((x+y) % 2 == 0) x<u else y<v
else (x+y) < (u+v) }.
zipWithIndex.foldLeft(array) {case (a,((x,y),i)) => a(y)(x) = i; a}
}
zigzag(8).foreach{
</lang>
ar => ar.foreach(x => print("%3d".format(x)))
println
}</lang>
 
=={{header|Seed7}}==
Anonymous user