Question

[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 match with parentheses, brackets must match to brackets, and curly braces must match to curly braces. Write a program to check that they are all balanced so humans don’t have to check themselves.

The requirements are:

- Must use Stack(s)

Example outputs:

print(balancing("a(a(a(a(")) # Output: False

print(balancing("()()()()()((())())")) # Output: True

print(balancing("((()))")) # Output: True

print(balancing("({}({)}")) # Output: False

print(balancing("[[[]]]()(){)")) # Output: False

print(balancing("")) # Output: True

print(balancing("def function(p1, p2)")) # Output: True

print(balancing("public int main(int argc, char[] argv) {(}")) # Output: False

print(balancing("[1, [1, [2],[3, 4], 5]]]")) # Output: False

Make a function as above. Write some tests. You will need to write your own Stack class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def balancing(s):
    stack = []
    for ch in s:
        if ch in "([{":
            stack.append(ch)
        elif ch == ']':
            if not stack or stack.pop() != '[':
                return False
        elif ch == ')':
            if not stack or stack.pop() != '(':
                return False
        elif ch == '}':
            if not stack or stack.pop() != '}':
                return False
    return len(stack) == 0


print(balancing("a(a(a(a("))  # Output: False
print(balancing("()()()()()((())())"))  # Output: True
print(balancing("((()))"))  # Output: True
print(balancing("({}({)}"))  # Output: False
print(balancing("[[[]]]()(){)"))  # Output: False
print(balancing(""))  # Output: True
print(balancing("def function(p1, p2)"))  # Output: True
print(balancing("public int main(int argc, char[] argv) {(}"))  # Output: False
print(balancing("[1, [1, [2],[3, 4], 5]]]"))  # Output: False
Add a comment
Know the answer?
Add Answer to:
[3] In python, please help me write the functions needed for this problem: Another Way to do Parentheses: For this probl...
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
  • Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...

    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...

  • need help with balancing brackets in python. I don't really understand it much. an unbalanced one...

    need help with balancing brackets in python. I don't really understand it much. an unbalanced one will look like this: (<x)>(())() For this exercise, you must write a function called balanced brackets brackets in the string, that is: '(' , ') and >', are correctly balanced. This function will be passed a string as an input, and you must check that any parentheses or angled Here are a few examples of strings where brackets are correctly balanced: a (<bcd>ef)g abcde...

  • 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...

  • In python, please help me fill in these functions given their limitations. Please use for loops....

    In python, please help me fill in these functions given their limitations. Please use for loops. def palindrome(x): """ For a given string, determine if the string is a palindrome. (The same forward and back) Input: String of any size (including empty) Return: Boolean (True if palindrome, False otherwise) Limitation: You must use a loop. Only function allowed to be used is len (if needed). Cannot use "in" besides "for var in container" """ pass def getCount(char, strng): """ Get...

  • Please give me that correct code for this by using C++ language. and i hope you...

    Please give me that correct code for this by using C++ language. and i hope you use Visual stduio Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }.  For example, the string  {a(b+ac)d[xy]g} 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...

  • Please help me write down a program to check email address valid or not. Tip:parse the...

    Please help me write down a program to check email address valid or not. Tip:parse the string and count the special characters like '@', '.' .and at least one character. Suggested libc functions: printf() 1.Have to be by the argc/argv of main function 2.Connot use scanf/getchar and other user input functions 3.Please explain the details and steps for me cause I'm a beginner hhhhhh Thank you soooo much for helping me!

  • please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must chec...

    please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must check if m is within the length of both s and t. int submatch(char* s, char* t, int n, int m) Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match....

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • Please help run all the examples correctly and only the examples without extra things in or...

    Please help run all the examples correctly and only the examples without extra things in or less things in, it should run EXACTLY 100% like the examples. C language ONLY. And please dont reply if you cant run all the examples given. We know the tr command allows you to replace or translate characters in of a stream. It takes in two arguments - a set of characters to be replaced and a set of replacement characters. The full functionality...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

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
ADVERTISEMENT