Created
September 17, 2014 00:02
-
-
Save dz1984/3c7e8fa5c3a379379f58 to your computer and use it in GitHub Desktop.
Check parentheses balancing on the "Functional Programming Principles in Scala" course in Coursera.
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
balance = (charList) -> | |
isBegin = (_char) -> _char == '(' | |
isEnd = (_char) -> _char == ')' | |
check = (_counter,_char) -> | |
return _counter+1 if isBegin(_char) | |
return _counter-2 if isEnd(_char) and (_counter<=0) | |
return _counter-1 if isEnd(_char) | |
return _counter | |
loops = (_counter, _charList) -> | |
if _.isEmpty _charList | |
return (_counter == 0) | |
else | |
loops(check(_counter, _.head _charList), (_.tail _charList)) | |
return loops(0,charList) | |
originText = "(ex)()am" | |
charList = _.toArray originText | |
console.log balance charList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment