The first exercise was pretty simple. Here is my solution:
Scala
1
2
3
4
5
6
def pascal(c: Int, r: Int): Int = {
def factorial(n: Int): Int =
if (n == 0) 1 else n * factorial(n - 1)
factorial(r) / (factorial(r - c) * factorial(c))
}
The second exercise was a little more challenging, but I was able to get it eventually, here is my solution:
Scala
1
2
3
4
5
6
7
8
9
10
def balance(chars: List[Char]): Boolean = {
def count(acc: Int, chars: List[Char]): Int =
if (chars.isEmpty || acc < 0) acc
else if (chars.head == '(') count(acc + 1, chars.tail)
else if (chars.head == ')') count(acc - 1, chars.tail)
else count(acc, chars.tail)
count(0, chars) == 0
}
This was very challenging, so much so I was not able to come up with a functional solution. After some research I was able to come up with an imperative solution:
Scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def countChange(money: Int, coins: List[Int]): Int = {
val c = new Array[Int](money + 1)
c(0) = 1
for (k <- 1 to money) {
c(k) = 0
}
for (k <- coins) {
for (i <- 0 to money - k) {
c(i + k) += c(i)
}
}
c(money)
}