You are viewing a single comment's thread. Return to all comments →
object Solution { def main(args: Array[String]) { val row = io.Source.stdin.getLines .take(1) .map(_.trim.toInt) .next() def triangle(row: Int): Unit = { def doT(row: Int, prev: List[Int]): Unit = { if (row == 0) return val next = prev.sliding(2, 1) .map(pair => pair.sum) .toList println (next mkString " ") doT(row - 1, (0 :: next) :+ 0) } doT(row, List(1)) } triangle(row) } }
Yeah, so I am not thrilled about this. Having to append and prepend zeros seems hacky.
Seems like cookies are disabled on this browser, please enable them to open this website
Pascal's Triangle
You are viewing a single comment's thread. Return to all comments →
Yeah, so I am not thrilled about this. Having to append and prepend zeros seems hacky.