Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created May 5, 2014 23:43
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 Jared-Prime/11550120 to your computer and use it in GitHub Desktop.
Save Jared-Prime/11550120 to your computer and use it in GitHub Desktop.
def balanced(chars: List[Char]): Boolean = {
def innerBalance(thisChar: Char, rest: List[Char], center: Int): Int = {
def shift(thisChar: String, center: Int): Int = {
if (thisChar == "(") {
center << 1
} else if (thisChar == ")") {
center >> 1
} else {
center
}
}
if (rest.isEmpty) {
shift(thisChar.toString, center)
} else {
innerBalance(rest.head, rest.tail, shift(thisChar.toString, center))
}
}
innerBalance(chars.head, chars.tail, 1) == 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment