Skip to content

Instantly share code, notes, and snippets.

@zspencer
Created January 14, 2011 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspencer/780211 to your computer and use it in GitHub Desktop.
Save zspencer/780211 to your computer and use it in GitHub Desktop.
The Twelve Days of Christmas
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)
}
}
}
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