Last active
August 29, 2015 14:00
-
-
Save fbiville/11345986 to your computer and use it in GitHub Desktop.
ScalaNoob - ProgFun Week1
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
def balance(chars: List[Char]): Boolean = { | |
val result:(Boolean, Int) = chars.foldLeft((true, 0))((tuple:(Boolean, Int), char:Char) => { | |
val count = balanceCount(tuple._2, char) | |
(tuple._1 && count >= 0, count) | |
}) | |
result._1 && result._2 == 0 | |
} | |
private def balanceCount(previousCount: Int, char: Char) : Int = { | |
char match { | |
case '(' => previousCount+1 | |
case ')' => previousCount-1 | |
case _ => previousCount | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment