Question

Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...

  1. Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example:

>>> parenthesesMatch('(){}[]')

True

>>> parenthesesMatch('{[()]}')

True

>>> parenthesesMatch('((())){[()]}')

True

>>> parenthesesMatch('(}')

False

>>> parenthesesMatch('({])')

False

>>> parenthesesMatch('((())')

False

>>> parenthesesMatch('(()))')

False

>>> 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:
    1. If the character is one of opening marks(,[,{ push it on the stack.
    2. If the character is one of the closing marks ),],} and the stack is empty, then there were not enough preceding opening marks, so return False.
    3. 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 }, return False, if they are of the same type, move on to the next char.
  3. Once the iteration is finished, you know that the parentheses match if and only if the stack is empty.

use python3.7

check your answer

>>> parenthesesMatch('(){}[]')
True
>>> parenthesesMatch('{[()]}')
True
>>> parenthesesMatch('((())){[()]}')
True
>>> parenthesesMatch('(}')
False
>>> parenthesesMatch(')(][') # right number, but out of order
False
>>> parenthesesMatch('([)]') # right number, but out of order
False
>>> parenthesesMatch('({])')
False
>>> parenthesesMatch('((())')
False
>>> parenthesesMatch('(()))')
False

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#method to check valid parentheses
def parenthesesMatch(str):

#initializing empty stack
stack = [];

#iterting string
for i in range(0,len(str)):

#if chracter is opening bracket
if(str[i]=='(' or str[i]=='{' or str[i]=='[' ):
#push on stack
stack.append(str[i])

#if character is closing bracket
else:
#if stack is empty
if(len(stack)<1):
#invalid parantheses
return False

#if string character is ')'
if(str[i]==')'):

#top of stack is not '('
if(stack[len(stack)-1]!='('):

#invalid parantheses
return False;

#removing top of stack
stack.pop();

#if string character is '}'   
if(str[i]=='}'):
  
#top of stack is not '{'
if(stack[len(stack)-1]!='{'):

#invalid parantheses
return False;

#removing top of stack
stack.pop();

#if string character is ']'   
if(str[i]==']'):
  
#top of stack is not '['
if(stack[len(stack)-1]!='['):
#invalid parantheses
return False;

#removing top of stack
stack.pop();

# if stack is not empty
if(len(stack)!=0):
# invalid parantheses
return False;

#otherwise valid parantheses
return True;
INDENTED CODE SCREENSHOT:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [3] In python, please help me write the functions needed for this problem: Another Way to do Parentheses: For this probl...

    [3] In python, please help me write the functions needed for this problem: Another Way to do Parentheses: For this problem, you will be given a string that contains parentheses, brackets, and curly braces ( (, ), [, ], {,} ) and other characters. As a human you are given a string of any characters, determine if the parentheses, brackets, and braces match. (No trailing or leading parentheses). The function called on an empty string will return True. Parentheses must...

  • Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...

    Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets [].  For example, the string {a(b+ac)d} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac)d} do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Write...

  • JAVA programming The task of parsing a file for correctness is an essential part of any...

    JAVA programming The task of parsing a file for correctness is an essential part of any programmer toolkit. The check for a balanced set of brackets in a file a Stack can be used and the following algorithm: • The source file is read character by character • Each time an opening '{' is read it is pushed onto the stack • Each time a closing '}' is read the stack is popped • If the stack is empty when...

  • Please use Java. Write a non-recursive method that uses a stack to check whether its string...

    Please use Java. Write a non-recursive method that uses a stack to check whether its string parameter is a palindrome. Write a program to test and demo your method. Fibonacci Numbers Write a method that uses a stack to determine the desired Fibonacci number. Write a program to test and demo your method. Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly...

  • This programming problem deals with the Linked List (LLStack) implementation. a. Create a simple user interface...

    This programming problem deals with the Linked List (LLStack) implementation. a. Create a simple user interface to allow a user to input an expression and store it as a string variable. b. Write a function using the LLStack implementation to check if the input string is a well-formed parenthetical expression. Assume (), {}, and [] are all parenthetical characters. Recall that a parenthetical expression is well-formed if i. All closing parentheses match the most recent, unmatched opening parentheses of the...

  • C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if...

    C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if the string contains the digits in descending order. The function can ignore (e.g. skip checking) all the characters that are not digits in the string. Empty string will return false. For example, string of “95421” or “862” or “8622” or “88” or “9” will return true and string of “95423” or “889” or “9445” or “449” or “” will return false.

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • On Python 3.6, write a function char_table(s), where s is a string. The function should produce...

    On Python 3.6, write a function char_table(s), where s is a string. The function should produce a table that shows the characters in the string and the number of occurrences of each character. Make the table nicely formatted, using right justification for the numbers (check out the format command.). List the characters in order (the order in the character encoding). Give some examples of the output.

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT