Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1.  
  2. /**
  3. * Exercise 1
  4. */
  5. def pascal(c: Int, r: Int): Int = {
  6. if (c == 0 || c == r) 1
  7. else pascal(c - 1, r - 1) + pascal(c, r - 1)
  8. }
  9.  
  10. /**
  11. * Exercise 2
  12. */
  13. def balance(chars: List[Char]): Boolean = {
  14. def balanced(chars: List[Char], open: Int): Boolean =
  15. if (chars.isEmpty) open == 0
  16. else if (chars.head == '(') balanced(chars.tail,open+1)
  17. else if (chars.head == ')') open>0 && balanced(chars.tail,open-1)
  18. else balanced(chars.tail,open)
  19. balanced(chars,0)
  20. }
  21.  
  22. /**
  23. * Exercise 3
  24. */
  25. def countChange(money: Int, coins: List[Int]): Int = {
  26. if (money < 0 ) 0
  27. else if (money == 0) 1
  28. else if (money >= 1 && coins.size <= 0) 0
  29. else countChange(money, coins.tail) + countChange(money - coins.head, coins)
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement