Guest User

Untitled

a guest
Feb 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package recfun
  2.  
  3. import scala.collection.mutable.ListBuffer
  4.  
  5. object Main {
  6. def main(args: Array[String]) {
  7. println("Pascal's Triangle")
  8. for (row <- 0 to 10) {
  9. for (col <- 0 to row)
  10. print(pascal(col, row) + " ")
  11. println()
  12. }
  13. }
  14.  
  15. /**
  16. * Exercise 1
  17. */
  18. def pascal(c: Int, r: Int): Int = {
  19. if (c == 0 || c == r) 1
  20. else pascal(c - 1, r - 1) + pascal(c, r - 1)
  21. }
  22.  
  23. /**
  24. * Exercise 2
  25. */
  26. def balance(chars: List[Char]): Boolean = {
  27. def f(chars: List[Char], num: Int): Boolean = {
  28. (chars,num) match {
  29. case (Nil,num) => return num == 0
  30. case (c :: tail,num) =>
  31. if(num < 0) return false
  32. if(c == '(') return f(tail,num+1)
  33. if(c == ')') return f(tail,num-1)
  34. else return f(tail,num)
  35. }
  36. }
  37. f(chars,0)
  38. }
  39.  
  40. /**
  41. * Exercise 3
  42. */
  43. def countChange(money: Int, coins: List[Int]): Int = {
  44. def count(m: Int, c: List[Int]) : Int = {
  45. if (c.isEmpty) 0
  46. else if (m - c.head == 0) 1
  47. else if (m - c.head < 0) 0
  48. else countChange(m - c.head, c) + countChange(m, c.tail)
  49. }
  50. count(money, coins.sorted)
  51. }
  52. }
Add Comment
Please, Sign In to add comment