Skip to content

Instantly share code, notes, and snippets.

@sortega
Created July 6, 2013 23:54
Show Gist options
  • Save sortega/5941704 to your computer and use it in GitHub Desktop.
Save sortega/5941704 to your computer and use it in GitHub Desktop.
Xmas tree kata
package xmas
object Tree {
def apply(n: Int) = {
val width = n * 2 - 1
def center(line: String) = {
val padding = " " * ((width - line.length) / 2)
padding ++ line ++ padding
}.mkString
def line(row: Int) = row * 2 + 1
center("*") +: Seq.tabulate(n) { row => center("0" * line(row)) }
}
}
package xmas
import org.scalatest.matchers.MustMatchers
import org.scalatest.FlatSpec
class TreeTest extends FlatSpec with MustMatchers{
"An empty tree" must "have just a star" in {
Tree(0) must equal (Seq("*"))
}
"A minimal tree" must "have a star and a zero" in {
Tree(1) must equal (Seq("*", "0"))
}
"A 2-tree" must "have 3 rows and 3 columns" in {
Tree(2) must equal (Seq(" * ",
" 0 ",
"000"))
}
"A 5-tree" must "have 6 rows and 9 columns" in {
Tree(5) must equal (Seq(
" * ",
" 0 ",
" 000 ",
" 00000 ",
" 0000000 ",
"000000000"
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment