Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Last active August 29, 2015 14:07
Show Gist options
  • Save guilleiguaran/b517167c3cd97fab6cb9 to your computer and use it in GitHub Desktop.
Save guilleiguaran/b517167c3cd97fab6cb9 to your computer and use it in GitHub Desktop.
def balance(chars: List[Char]): Boolean = {
def balanceIter(chars: List[Char], count: Int) : Boolean =
if(count < 0) false
else if(chars.isEmpty) count == 0
else if(chars.head == '(') balanceIter(chars.tail, count + 1)
else if(chars.head == ')') balanceIter(chars.tail, count - 1)
else balanceIter(chars.tail, count)
balanceIter(chars, 0)
}
def countChange(money: Int, coins: List[Int]): Int = {
if(money < 0 || coins.isEmpty) 0
else if(money == 0) 1
else countChange(money - coins.head, coins) + countChange(money, coins.tail)
}
def pascal(c: Int, r: Int): Int = {
if(c == 0 || r == 0 || c == r) 1
else pascal(c - 1, r - 1) + pascal(c, r - 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment