Created
July 6, 2013 23:54
-
-
Save sortega/5941704 to your computer and use it in GitHub Desktop.
Xmas tree kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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