Created
January 14, 2011 20:51
-
-
Save zspencer/780211 to your computer and use it in GitHub Desktop.
The Twelve Days of Christmas
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 twelvedays | |
import scala.annotation.tailrec | |
class TwelveDays { | |
@tailrec | |
final def calculateGiftsGivenOnDay(day: Int, totalGifts: Int = 0): Int = { | |
if (day == 1) { | |
totalGifts + day | |
} else { | |
calculateGiftsGivenOnDay(day - 1, totalGifts + day) | |
} | |
} | |
} | |
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 zms.GameOfLife | |
import org.scalatest.Spec | |
import org.scalatest.matchers.ShouldMatchers | |
import org.scalatest.junit.JUnitRunner | |
import org.junit.runner.RunWith | |
@RunWith(classOf[JUnitRunner]) | |
class GameTests extends Spec with ShouldMatchers { | |
describe("all days") { | |
it("first day should have 1 gift") { | |
val td = new TwelveDays() | |
td.calculateGiftsGivenOnDay(1) should be(1) | |
} | |
it("second day should have 3 gift") { | |
val td = new TwelveDays() | |
td.calculateGiftsGivenOnDay(2) should be(3) | |
} | |
it("third day should have 6 gifts") { | |
val td = new TwelveDays() | |
td.calculateGiftsGivenOnDay(3) should be (6) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment