Created
May 29, 2010 23:04
-
-
Save jamescarr/418612 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import org.specs._ | |
| object TriangleWordIdentifierSpec extends Specification{ | |
| "letter to number converter" should { | |
| "convert A to 1" in { | |
| convertLetterToNumber('A') must be(1) | |
| } | |
| "convert a to 1" in { | |
| convertLetterToNumber('a') must be(1) | |
| } | |
| "convert C to 3" in { | |
| convertLetterToNumber('C') must be(3) | |
| } | |
| "convert c to 3" in { | |
| convertLetterToNumber('c') must be(3) | |
| } | |
| } | |
| "generate word value" should { | |
| "return sum of each character for 'abc'" in { | |
| wordValue("abc") must be(1 + 2 + 3) | |
| } | |
| "return sum of each character for 'sky'" in { | |
| wordValue("Sky") must be(55) | |
| } | |
| "'ddd' should equal 4 + 4 + 4" in { | |
| wordValue("ddd") must be (4 + 4 + 4) | |
| } | |
| } | |
| } | |
| --- code: | |
| object TriangleWordIdentifier{ | |
| def isTriangleWord(word:String) = { | |
| true | |
| } | |
| } | |
| object convertLetterToNumber extends (Char => Int){ | |
| override def apply(s:Char) = s.toUpperCase.toInt - ('A'.toInt-1) | |
| } | |
| object wordValue extends (String => Int){ | |
| override def apply(s:String) = { | |
| List.fromString(s).foldRight(0)((a,b) => convertLetterToNumber(a)+b) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment