Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package recfun
- 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 = {
- if (c == 0 || r == c) 1
- else pascal(c - 1, r - 1) + pascal(c, r - 1)
- }
- /**
- * Exercise 2
- */
- def balance(chars: List[Char]): Boolean = {
- def isOpen(char: Char): Boolean =
- char == '('
- def isClose(char: Char): Boolean =
- char == ')'
- def balanceIter(_chars: List[Char], acc: Int): Boolean =
- if (_chars.isEmpty) acc == 0
- else if (acc < 0) false
- else if (isOpen(_chars.head)) balanceIter(_chars.tail, acc + 1)
- else if (isClose(_chars.head)) balanceIter(_chars.tail, acc - 1)
- else balanceIter(_chars.tail, acc)
- balanceIter(chars, 0)
- }
- /**
- * Exercise 3
- */
- def countChange(money: Int, coins: List[Int]): Int = {
- if (money == 0) 1
- else if (money < 0 || coins.isEmpty) 0
- else countChange(money - coins.head, coins) + countChange(money, coins.tail)
- }
- }
Add Comment
Please, Sign In to add comment