Skip to content

Instantly share code, notes, and snippets.

@goungoun
Created January 21, 2018 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goungoun/c0e2e646aa11cc8f95308c8905f9edab to your computer and use it in GitHub Desktop.
Save goungoun/c0e2e646aa11cc8f95308c8905f9edab to your computer and use it in GitHub Desktop.
balance
/**
* Exercise 2
*/
def balance(chars: List[Char]): Boolean = {
def num (x: Char): Int = x match {
case ')' => 1
case '(' => -1
case _ => 0
}
def sum(n: List[Int]): Int = {
val tmp = if (n.isEmpty) 0 else n.head + sum (n.tail)
if (tmp < 0) tmp-1 else tmp
}
(sum(chars.map (x => num(x))) == 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment