Question

In: Computer Science

python IMPORTANT : For this exercise, you will be defining a function that USES the Stack...

python

IMPORTANT : For this exercise, you will be defining a function that USES the Stack ADT. A stack implementation is provided to you as part of this exercise - you should not use your own Stack class. Instead, simply use the functions: Stack(), push(), pop() and is_empty() where necessary inside your function definition.

For this exercise, you must write a function called balanced_brackets(). This function will be passed a string as an input, and you must check that any parentheses or angled brackets in the string, that is: '(', '<', ')' and '>', are correctly balanced.

Here are a few examples of strings where brackets are correctly balanced:

a(<bcd>ef)g 
abcde 
a(b)c<<d>e(fg)>

and here are a few examples where the brackets are not balanced:

ab(cde> 
a<bc>)def<g> 
ab)c

Your balanced_brackets() function should return True if the input string is balanced, and False otherwise. Remember, you can assume that an implementation of the Stack ADT is available to you. It is therefore likely that your function definition will begin as follows:

def balanced_brackets(text):  
    s = Stack()  
    ...

For example:

Test Result
print(balanced_brackets('(<x>)(())()'))
True
print(balanced_brackets('x(y)z'))
True

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

===========================================================================

def balanced_brackets(text):
    s = Stack()

    for letter in text:
        if letter in ['(', '<']:
            s.push(letter)
        if letter in [')', '>']:
            if s.is_empty():
                return False
            top_letter = s.pop()
            if (letter == ')' and top_letter == '(') or (letter == '>' and top_letter == '<'):
                continue
            else:
                return False
    if not s.is_empty():
        return False
    return True


print(balanced_brackets('(<x>)(())()'))
print(balanced_brackets('x(y)z'))


Related Solutions

For this exercise, you will be defining a function which USES both the Stack and the...
For this exercise, you will be defining a function which USES both the Stack and the Queue ADTs. Your code can make use of any of the Queue ADT methods: Queue(), enqueue(), dequeue(), peek(), size() and is_empty() and any of the Stack ADT methods: Stack(), push(), pop(), peek(), size() and is_empty(). Write a function called mirror_queue(a_queue) which takes a Queue as a parameter. The function must modify the parameter Queue object so that the original queue items appear in their...
Write a Python function that receives a stack object s, where the items in the stack...
Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions: the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called. The function cannot use any additional variables of any of...
python: Implement a function that reverses a list of elements by pushing them onto a stack...
python: Implement a function that reverses a list of elements by pushing them onto a stack in one order, and writing them back to the list in reversed order.
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to...
Write a function that uses a local char queue and a local char stack to determine...
Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like: #include <stack> #include <queue> ... bool isPalindrome(const string& candidate) { stack<char> s; queue<char> q; //add only upper case letters and digits to the stack and the queue //if a letter is not upper case then convert it to upper case   } Note: I need help to write out this problem in C++...
Stack ADT What would you say is the most important drawback of using the stack that...
Stack ADT What would you say is the most important drawback of using the stack that should be considered before choosing it for use in a real application? Typed out please.
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT