Advertisement
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 || c == r) 1 else (pascal(c - 1, r - 1) + pascal(c, r - 1))
- /**
- * Exercise 2
- */
- def balance(chars: List[Char]): Boolean = {
- require(!chars.isEmpty)
- def countParentheses(chars: List[Char], acc: Int): Int = {
- if (acc < 0) -1
- else if (chars.isEmpty) acc
- else countParentheses(chars.tail, acc + typeChar(chars.head))
- }
- def typeChar(char: Char): Int = {
- if (char == '\u0028') 1
- else if (char == '\u0029') -1
- else 0
- }
- (countParentheses(chars, 0) >= 0)
- }
- /**
- * Exercise 3
- */
- def countChange(money: Int, coins: List[Int]): Int = {
- if (money < 0) 0
- else if (money == 0) 1
- else if (coins.isEmpty) 0
- else (countChange(money - coins.head, coins) + countChange(money, coins.tail))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement