1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alina [70]
3 years ago
5

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

Computers and Technology
1 answer:
zimovet [89]3 years ago
6 0

Answer:

Check the explanation

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.

You might be interested in
CHALLENGE ACTIVITY 7.3.1: Functions: Factoring out a unit-conversion calculation. Write a function so that the main() code below
77julia77 [94]

Answer:

See Explaination

Explanation:

#include <stdio.h>

MphAndMinutesToMiles()

{

double milesPerHour;

double minutesTraveled;

double hoursTraveled;

double milesTraveled;

scanf("%lf", &milesPerHour);

scanf("%lf", &minutesTraveled);

hoursTraveled = minutesTraveled / 60.0;

milesTraveled = hoursTraveled * milesPerHour;

printf("Miles: %lf\n", milesTraveled);

}

int main()

{

MphAndMinutesToMiles();

return 0;

}

4 0
4 years ago
6. What is the difference between portrait and landscape orientation? What are the advantages of
vlabodo [156]

Answer:

PORTRAIT ORIENTATION IS VERTICAL AND LANDSCAPE ORIENTATION IS HORIZONTAL

Explanation:

PORTRAIT SETTING CAN BE USED TO DESCRIBE MEANINGS OF LETTERS IN A WORD LANDSCAPE CAN BE USED TO DIFFERENTIATE  BETWEEN WORDS MEANINGS                                                                                                                                            

3 0
3 years ago
Select the correct answer.
sashaice [31]

Answer:

A. Project Manager

Explanation:

3 0
4 years ago
Read 2 more answers
How to add links in HTML? ​
saveliy_v [14]

Answer:

<a href="url">link text</a>

6 0
2 years ago
3. with a dui charge on a driver’s record ______________. a. the price of his or her insurance greatly increases b. he or she ma
djyliett [7]
<span>The answer to your question is D. both A and C</span>
7 0
4 years ago
Other questions:
  • What is the f(n) runtime of the following pseudocode: sum-0 for A = N/2 downto 1 for B-1 t increment sum by B Explain: exactly w
    13·1 answer
  • Who is known as the father of computer? ​
    6·2 answers
  • I am doing Microsoft Excel and I have do formulas, Can some please explain to me how do them?
    14·1 answer
  • Which type of address is used in a packet to address the packet to a single host??
    12·1 answer
  • 4-One possible performance multiplication enhancement is to do a shift and add instead of an actual multiplication. Since 9 * 6,
    12·1 answer
  • Brad is joining a big website design firm. He is worried that he may not be able to adapt to the company culture. What can he do
    15·1 answer
  • Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4,999. The drive is currently serving a request at cylinder 2,150,
    6·1 answer
  • Give the value of the zero flag, the carry flag, the signflag, and the overflow flag after each of the following instructions if
    8·1 answer
  • You recently discovered that several key files of your antivirus program have been deleted. you suspect that a virus has deleted
    9·1 answer
  • _________ analysis is a data mining application that discovers co-occurrence relationships among activities performed by specifi
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!