diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-03-28 22:12:53 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:21 +0100 |
commit | 8d91a5a9141393e6c738d8624f7fe5d1bbdb28f9 (patch) | |
tree | a51880194eb31b00611be8dd6d713d3072bd2f2f /Scala/recfun/src | |
parent | 6b137fb0ee01b203f4008ab85ad88a1c1aec3cbe (diff) | |
download | coursera-8d91a5a9141393e6c738d8624f7fe5d1bbdb28f9.zip coursera-8d91a5a9141393e6c738d8624f7fe5d1bbdb28f9.tar.gz |
Scala : add recfun
Diffstat (limited to 'Scala/recfun/src')
-rw-r--r-- | Scala/recfun/src/main/scala/common/package.scala | 40 | ||||
-rw-r--r-- | Scala/recfun/src/main/scala/recfun/Main.scala | 28 | ||||
-rw-r--r-- | Scala/recfun/src/test/scala/recfun/BalanceSuite.scala | 27 | ||||
-rw-r--r-- | Scala/recfun/src/test/scala/recfun/CountChangeSuite.scala | 26 | ||||
-rw-r--r-- | Scala/recfun/src/test/scala/recfun/PascalSuite.scala | 22 |
5 files changed, 143 insertions, 0 deletions
diff --git a/Scala/recfun/src/main/scala/common/package.scala b/Scala/recfun/src/main/scala/common/package.scala new file mode 100644 index 0000000..f1c74c3 --- /dev/null +++ b/Scala/recfun/src/main/scala/common/package.scala @@ -0,0 +1,40 @@ +import java.io.File + +package object common { + + /** An alias for the `Nothing` type. + * Denotes that the type should be filled in. + */ + type ??? = Nothing + + /** An alias for the `Any` type. + * Denotes that the type should be filled in. + */ + type *** = Any + + + /** + * Get a child of a file. For example, + * + * subFile(homeDir, "b", "c") + * + * corresponds to ~/b/c + */ + def subFile(file: File, children: String*) = { + children.foldLeft(file)((file, child) => new File(file, child)) + } + + /** + * Get a resource from the `src/main/resources` directory. Eclipse does not copy + * resources to the output directory, then the class loader cannot find them. + */ + def resourceAsStreamFromSrc(resourcePath: List[String]): Option[java.io.InputStream] = { + val classesDir = new File(getClass.getResource(".").toURI) + val projectDir = classesDir.getParentFile.getParentFile.getParentFile.getParentFile + val resourceFile = subFile(projectDir, ("src" :: "main" :: "resources" :: resourcePath): _*) + if (resourceFile.exists) + Some(new java.io.FileInputStream(resourceFile)) + else + None + } +} diff --git a/Scala/recfun/src/main/scala/recfun/Main.scala b/Scala/recfun/src/main/scala/recfun/Main.scala new file mode 100644 index 0000000..277aa1a --- /dev/null +++ b/Scala/recfun/src/main/scala/recfun/Main.scala @@ -0,0 +1,28 @@ +package recfun +import common._ + +object Main { + def main(args: Array[String]) { + println("Pascal's Triangle") + for (row <- 0 to 10) { + for (col <- 0 to row) + print(pascal(col, row) + " ") + println() + } + } + + /** + * Exercise 1 + */ + def pascal(c: Int, r: Int): Int = ??? + + /** + * Exercise 2 + */ + def balance(chars: List[Char]): Boolean = ??? + + /** + * Exercise 3 + */ + def countChange(money: Int, coins: List[Int]): Int = ??? +} diff --git a/Scala/recfun/src/test/scala/recfun/BalanceSuite.scala b/Scala/recfun/src/test/scala/recfun/BalanceSuite.scala new file mode 100644 index 0000000..c040682 --- /dev/null +++ b/Scala/recfun/src/test/scala/recfun/BalanceSuite.scala @@ -0,0 +1,27 @@ +package recfun + +import org.scalatest.FunSuite + +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class BalanceSuite extends FunSuite { + import Main.balance + + test("balance: '(if (zero? x) max (/ 1 x))' is balanced") { + assert(balance("(if (zero? x) max (/ 1 x))".toList)) + } + + test("balance: 'I told him ...' is balanced") { + assert(balance("I told him (that it's not (yet) done).\n(But he wasn't listening)".toList)) + } + + test("balance: ':-)' is unbalanced") { + assert(!balance(":-)".toList)) + } + + test("balance: counting is not enough") { + assert(!balance("())(".toList)) + } +} diff --git a/Scala/recfun/src/test/scala/recfun/CountChangeSuite.scala b/Scala/recfun/src/test/scala/recfun/CountChangeSuite.scala new file mode 100644 index 0000000..0da3359 --- /dev/null +++ b/Scala/recfun/src/test/scala/recfun/CountChangeSuite.scala @@ -0,0 +1,26 @@ +package recfun + +import org.scalatest.FunSuite + +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class CountChangeSuite extends FunSuite { + import Main.countChange + test("countChange: example given in instructions") { + assert(countChange(4,List(1,2)) === 3) + } + + test("countChange: sorted CHF") { + assert(countChange(300,List(5,10,20,50,100,200,500)) === 1022) + } + + test("countChange: no pennies") { + assert(countChange(301,List(5,10,20,50,100,200,500)) === 0) + } + + test("countChange: unsorted CHF") { + assert(countChange(300,List(500,5,50,100,20,200,10)) === 1022) + } +} diff --git a/Scala/recfun/src/test/scala/recfun/PascalSuite.scala b/Scala/recfun/src/test/scala/recfun/PascalSuite.scala new file mode 100644 index 0000000..58c899e --- /dev/null +++ b/Scala/recfun/src/test/scala/recfun/PascalSuite.scala @@ -0,0 +1,22 @@ +package recfun + +import org.scalatest.FunSuite + +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class PascalSuite extends FunSuite { + import Main.pascal + test("pascal: col=0,row=2") { + assert(pascal(0,2) === 1) + } + + test("pascal: col=1,row=2") { + assert(pascal(1,2) === 2) + } + + test("pascal: col=1,row=3") { + assert(pascal(1,3) === 3) + } +} |