Created
January 21, 2018 13:35
-
-
Save goungoun/c0e2e646aa11cc8f95308c8905f9edab to your computer and use it in GitHub Desktop.
balance
This file contains 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
/** | |
* 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