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