<jupyter_start><jupyter_text>```Algorithm ParenMatch(X,n):Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a numberOutput: true if and only if all the grouping symbols in X match=======================================Let S be an empty stackfor i=0 to n-1 do	if X[i] is an opening grouping symbol then		S.push(X[i])	else if X[i] is a closing grouping symbol then		if S.is_empty() then			return false {nothing to match with} 		if S.pop() does not match the type of X[i] then			return false {wrong type} if S.is_empty() then	return true {every symbol matched} else return false {some symbols were never matched}```<jupyter_code>def ParenMatch(X):
    lefty = '({['
    righty = ')}]'
    S = ArrayStack()
    for symbol in X:
        if symbol in lefty:
            S.push(symbol)
        elif symbol in righty:
            if S.is_empty():
                return False
            if righty.index(symbol) != lefty.index(S.pop()):
                return False
    return S.is_empty()
print(ParenMatch(list('()(()){([()])}')))
print(ParenMatch(list('((()(()){([()])}))')))
print(ParenMatch(list(')(()){([()])}')))
print(ParenMatch(list('({[])}')))
print(ParenMatch(list('(')))
def ParenMatch_explained(X):
    print(f'Processing {X}')
    lefty = '({['
    righty = ')}]'
    S = ArrayStack()
    index = 0
    for symbol in X:
        print(f'#{index} - Processing {symbol}')
        index += 1
        
        if symbol in lefty:
            S.push(symbol)
            print(f'Push {symbol} into the stack')
        elif symbol in righty:
            if S.is_empty():
                print('The stack is empty, but there comes a righty')
                return False
            popped = S.pop()
            print(f'Pop {popped} from the stack')            
            if righty.index(symbol) != lefty.index(popped):                
                print(f'The righty {symbol} is not matching to {popped}')
                return False
            print(f'The popped symbol {popped} is matched to {symbol}')
        S.display()
    return S.is_empty()
print(ParenMatch_explained(list('()(()){([()])}')))
print(ParenMatch_explained(list('((()(()){([()])}))')))
print(ParenMatch_explained(list(')(()){([()])}')))
print(ParenMatch_explained(list('({[])}')))
print(ParenMatch_explained(list('(')))<jupyter_output>Processing ['(']
#0 - Processing (
Push ( into the stack
STACK: B|(|T
False
{'fraction_non_alphanumeric': 0.17647058823529413, 'fraction_numerical': 0.002042483660130719, 'mean_word_length': 2.875, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '600527', 'license': 'MIT', 'n_tokens_mistral': 794, 'n_tokens_neox': 771, 'n_words': 227, 'path': 'Lecture Codes/Lecture 3 notebooks/Parentheses_Matching.ipynb', 'repo_name': 'h0han/SE274_DataStructure', 'size': '2448'}