Question: Please give me the solution Thanks! 4. Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly
Please give me the solution Thanks!
4. Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces. i.e., the characters in , ( [ { } ] ) , , retums True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack For, example >>> paenthesesMatchl' Ol1i') True >>> paenthesesMatchl'i[)]') True >>> paenthesesMatchl' ((O))l[O]!') True >>par Fa 1 3e >>> parenthe 3esMatch!') (] [') # right number, but out of order Fa 1 3e >>> parenthe se3Match ("(D]') # right number, but out of order >>> paenthesesMatchl' ((])') Fa 1 3e >>> paenthesesMatchl' (())') Fa 1 3e >>> paenthesesMatchl'(O))') Fa 1 3e Hint: It is not sufficient to just count the number of opening and closing marks. But, it is easy to write this as a simple application of the Stack class. Here is an algorithm 1. Create an empty stack. 2. Iterate over the characters in the given string a. If the character is one of opening marks.(, [, t push it on the stack. b. If the character is one of the closing marks),1, > and the stack is empty, then there were not enough preceding opening marks, so return False If the character is a closing mark and the stack is not empty, pop an (opening) mark from the stack. If they are not of the same type, ie.. ( and) or [ and ] or { and 3, retum False, if they are of the same type, move on to the next char c. 3. Once the iteration is finished, you know that the parentheses match if and only if the stackis empty 4. Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces. i.e., the characters in , ( [ { } ] ) , , retums True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack For, example >>> paenthesesMatchl' Ol1i') True >>> paenthesesMatchl'i[)]') True >>> paenthesesMatchl' ((O))l[O]!') True >>par Fa 1 3e >>> parenthe 3esMatch!') (] [') # right number, but out of order Fa 1 3e >>> parenthe se3Match ("(D]') # right number, but out of order >>> paenthesesMatchl' ((])') Fa 1 3e >>> paenthesesMatchl' (())') Fa 1 3e >>> paenthesesMatchl'(O))') Fa 1 3e Hint: It is not sufficient to just count the number of opening and closing marks. But, it is easy to write this as a simple application of the Stack class. Here is an algorithm 1. Create an empty stack. 2. Iterate over the characters in the given string a. If the character is one of opening marks.(, [, t push it on the stack. b. If the character is one of the closing marks),1, > and the stack is empty, then there were not enough preceding opening marks, so return False If the character is a closing mark and the stack is not empty, pop an (opening) mark from the stack. If they are not of the same type, ie.. ( and) or [ and ] or { and 3, retum False, if they are of the same type, move on to the next char c. 3. Once the iteration is finished, you know that the parentheses match if and only if the stackis empty
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
