Created
November 23, 2013 02:03
-
-
Save blaisebaileyfinnegan/7609870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.annotation.tailrec | |
| def pascal(c: Int, r: Int): Int = { | |
| if ((c > r) || (c < 0)) 0 | |
| else if (r == 0) 1 | |
| else pascal(c - 1, r - 1) + pascal(c, r - 1) | |
| } | |
| def balance(chars: List[Char]): Boolean = { | |
| @tailrec | |
| def check(chars: List[Char], open: Int): Boolean = { | |
| if (open < 0) false | |
| else if (chars.isEmpty) open == 0 | |
| else if (chars.head == '(') check(chars.tail, open + 1) | |
| else if (chars.head == ')') check(chars.tail, open - 1) | |
| else check(chars.tail, open) | |
| } | |
| check(chars, 0) | |
| } | |
| val list = List( | |
| "(if (zero ? x) max (/ 1 x))", | |
| "I told him (that it's not (yet) done). (But he wasn't listening)", | |
| ":-)", | |
| "())(", | |
| "())", | |
| "()((()") | |
| list.foreach((seq: String) => { | |
| println(balance(seq.toList)) | |
| }) | |
| def countChange(money: Int, coins: List[Int]): Int = { | |
| if (coins.isEmpty || (money < 0)) 0 | |
| else if (money == 0) 1 | |
| else countChange(money, coins.tail) + | |
| countChange(money - coins.head, coins) | |
| } | |
| val coins = List(1, 5, 10, 25, 50) | |
| 0 to 100 foreach ((x: Int) => println(x + ": " + countChange(x, coins))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment