Skip to content

Instantly share code, notes, and snippets.

@vabock
Created June 10, 2011 21:05
Show Gist options
  • Save vabock/1019776 to your computer and use it in GitHub Desktop.
Save vabock/1019776 to your computer and use it in GitHub Desktop.
import scala.collection.Seq
object Main {
def main(args: Array[String]): Unit = {
dim(readInt)
}
def dim(num: Int): Unit = {
def put[A](xs: Seq[A]) = println(xs mkString " ")
def nop(c: Int) = {
require(c >= 0); List.fill(c){0}
}
def tri(n: Int) = {
val l = n / 2 - 1
val r = l + (n & 1)
(0 to l) ++ (r to 0 by -1)
}
def row(pos: Int, num: Int) = {
val z = nop(pos)
z ::: (if (num - pos * 2 <= 1) num :: Nil
else num +: nop(num - pos * 2 - 2) :+ num) ::: z
}
tri(num).foreach(x => put(row(x, num)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment