36.4k views
4 votes
In this section, you will use a stack to implement a program that checks if a string is balanced. Use the skeleton code provided below to accept inputs via command line arguments. Do not modify how the inputs are accepted as this is how we will test your code. Be sure you handle a NULL input string; you can either consider a NULL string as balanced or ensure that the input string cannot be empty. You are to fill in the missing code for is_balanced() which must be implemented with a stack (no counter integers or string functions are allowed). If you use a counter or string operation of any kind, you will not receive credit for completing this part of the assignment. You may use Python's implementation of a list and you may add additional functions as you see fit.you may add additional functions as you see fit.

For running the code use: python balance.py "{}"

Skeleton code:

import sys


# Checks whether the input string is balanced
# param: input string
# returns True if string is balanced, otherwise returns False
def is_balanced(input_string):

# initialize an empty list as the stack
stack = []

# iterate over each character in the string
for i in input_string:

# FIXME: You will write this function

return False


if __name__ == '__main__':
# get input string
_input_string = sys.argv[1] # DO NOT MODIFY

balanced = is_balanced(_input_string)

if balanced:
print("The string {} is balanced".format(_input_string))
else:
print("The string {} is not balanced".format(_input_string))

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

#source code:

import sys

def is_balanced(input_string):

stack = []

for i in input_string:

if(i=="{"):

stack.append("{")

elif(i=="}"):

stack.pop()

if(len(stack)==0):

return True

else:

return False

if __name__ == '__main__':

try:

_input_string = sys.argv[1]

balanced = is_balanced(_input_string)

if balanced:

print("The string {} is balanced".format(_input_string))

else:

print("The string {} is not balanced".format(_input_string))

except:

print("String can't be empty")

Kindly check the attached image below to see the code screenshot and code output.

In this section, you will use a stack to implement a program that checks if a string-example-1
In this section, you will use a stack to implement a program that checks if a string-example-2
User Garo Yeriazarian
by
6.9k points