Skip to content

Commit

Permalink
Solve 1st assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-myltsev committed Dec 10, 2013
1 parent 3006446 commit 5499f32
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
28 changes: 24 additions & 4 deletions a1-recfun/src/main/scala/recfun/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,35 @@ object Main {
/**
* Exercise 1
*/
def pascal(c: Int, r: Int): Int = ???
def pascal(column: Int, row: Int): Int = (column, row) match {
case (c, r) if r < c => throw new IllegalArgumentException("must be: r >= c")
case (c, r) if (column == 0 || column == row) => 1
case _ => pascal(column, row - 1) + pascal(column - 1, row - 1)
}

/**
* Exercise 2
*/
def balance(chars: List[Char]): Boolean = ???
def balance(chars: List[Char]): Boolean = {
def parenthesesCounter(cs: List[Char], leftParenthesesCount: Int): Boolean =
if (leftParenthesesCount < 0) false
else cs match {
case Nil => leftParenthesesCount == 0
case ')' :: xs => parenthesesCounter(xs, leftParenthesesCount - 1)
case '(' :: xs => parenthesesCounter(xs, leftParenthesesCount + 1)
case x :: xs => parenthesesCounter(xs, leftParenthesesCount)
}
parenthesesCounter(chars, 0)
}

/**
* Exercise 3
*/
def countChange(money: Int, coins: List[Int]): Int = ???
}
def countChange(money: Int, coins: List[Int]): Int = {
def countChangeWays(money: Int, coins: List[Int]): Int =
if (money == 0) 1
else if (coins.isEmpty || money < 0) 0
else countChangeWays(money, coins.tail) + countChangeWays(money - coins.head, coins)
countChangeWays(money, coins.sorted)
}
}
2 changes: 1 addition & 1 deletion a1-recfun/src/test/scala/recfun/BalanceSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class BalanceSuite extends FunSuite {
test("balance: counting is not enough") {
assert(!balance("())(".toList))
}
}
}
10 changes: 5 additions & 5 deletions a1-recfun/src/test/scala/recfun/CountChangeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import org.scalatest.junit.JUnitRunner
class CountChangeSuite extends FunSuite {
import Main.countChange
test("countChange: example given in instructions") {
assert(countChange(4,List(1,2)) === 3)
assert(countChange(4, List(1, 2)) === 3)
}

test("countChange: sorted CHF") {
assert(countChange(300,List(5,10,20,50,100,200,500)) === 1022)
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)
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)
assert(countChange(300, List(500, 5, 50, 100, 20, 200, 10)) === 1022)
}
}
}
12 changes: 9 additions & 3 deletions a1-recfun/src/test/scala/recfun/PascalSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import org.scalatest.junit.JUnitRunner
class PascalSuite extends FunSuite {
import Main.pascal
test("pascal: col=0,row=2") {
assert(pascal(0,2) === 1)
assert(pascal(0, 2) === 1)
}

test("pascal: col=1,row=2") {
assert(pascal(1,2) === 2)
assert(pascal(1, 2) === 2)
}

test("pascal: col=1,row=3") {
assert(pascal(1,3) === 3)
assert(pascal(1, 3) === 3)
}

test("pascal: row < col") {
intercept[IllegalArgumentException] {
pascal(3, 1)
}
}
}

0 comments on commit 5499f32

Please sign in to comment.