Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created September 17, 2014 00:02
Show Gist options
  • Save dz1984/3c7e8fa5c3a379379f58 to your computer and use it in GitHub Desktop.
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.
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