Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active August 29, 2015 14:00
Show Gist options
  • Save fbiville/11345986 to your computer and use it in GitHub Desktop.
Save fbiville/11345986 to your computer and use it in GitHub Desktop.
ScalaNoob - ProgFun Week1
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