vitoralbano
BAN USERi've tried by this way, checking premature closure with wrong syntax
Python 2.7
[update] Just a fix, close parenthesis at close chars.
def check_closures(input):
open_chars = '[{(<'
close_chars = ']})>'
balance = [0] * len(open_chars)
input = re.sub(r'[^][{}<>()]', '', input)
print('input', input)
if(bool(len(input)%2)):
return False
closures = {}
last_opened = False
char_closure_index = None
for char in input:
char_closure_index = open_chars.find(char)
if(char_closure_index > -1):
last_opened = True
balance[char_closure_index] += 1
elif(char in close_chars):
char_closure_index = close_chars.find(char)
if(last_opened):
possible_close = close_chars[char_closure_index]
if(char is not possible_close):
return False
balance[char_closure_index] -= 1
last_opened = False
if(balance[char_closure_index] < 0):
return False
return sum(balance) == 0
print(check_closures('[{}]'))
print(check_closures('{{}}{)([][]'))
Over my interpretation, most frequent is different of longest sequence.
And i have a question, if there is more elements with the same frequency? Should return a list of them?
- vitoralbano January 14, 2018