Question

Python coding

The Gocargo are automating their testing works of the logistics wing too. They have planned to test if the container loading works correctly or not.

Given the process involved in loading, in the form of expression, evaluate if the container loader has worked correct or not.


Conventions

'{' for cargos, '[' for containers and '(' for boxes.


Rules:

* cargo cannot be inside another cargo or container or box.

* container cannot be inside another container or box.

* box can be inside another box.


Please check whether the container loader has worked correctly.


Sample Input 1 :


{[2+3+4+(2+1)+4+(2+4)]+[3+4]}+{[2+4+5]}


 Sample Output 1:

Valid


Sample Input 2 :

{[2+3+4+(2+1)+4+(2+4)]+3+4]}+{[2+4+5]}}

Sample Output 2:

Invalid


Explanation for sample 1:

There are two cargos.

In the first cargo, there are two containers.

     The first container contains two boxes, plus some other items.

     The second container contains no boxes, but two items.

In the second cargo, there is one container with 3 items.

It follows the rules. Therefore, the container loader has worked correctly.


Explanation for sample 2:

The unbalanced brackets shows that the container loader has not worked correctly.


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

import sys



exp=input()

flag=0

for i in range(0,len(exp),1):

  if(exp[i]=="{"):

    flag+=1

  if(exp[i]=="}"):

    flag-=1

  if(flag>1 or flag<=-1):

    print("Invalid")

    sys.exit()


flag=0

con=0

box=0

for i in range(0,len(exp),1):

  if(exp[i]=='{'):

    flag+=1

  if (exp[i] == "}"):

    flag -= 1

  if (exp[i] == '('):

    box += 1

  if (exp[i] == ")"):

    box -= 1

  if(exp[i]=='['):

    con+=1

  if(exp[i]==']'):

    con-=1

  if(con>flag):

    print("Invalid")

    sys.exit()

  if(con>flag or box<=-1):   #box>con box>flag or

    print("Invalid")

    sys.exit()


flag=0

con=0

box=0


for i in range(0,len(exp),1):

  if(exp[i]=="["):

    con+=1

  if(exp[i]=="]"):

    con-=1

  if(con>1):

    print("Invalid")

    sys.exit()

  if(exp[i]=="("):

    box+=1

  if(exp[i]==")"):

    box-=1

  if(con>1 or con<=-1 or box<=-1):  #or box>con

    print("Invalid")

    sys.exit()


#({})

flag=0

box=0

for i in range(0,len(exp),1):

  if(exp[i]=="{"):

    flag+=1

  if(exp[i]=="}"):

    flag-=1

  if(exp[i]=="("):

    box+=1

  if(exp[i]==")"):

    box-=1

  if(box>flag):

    print("Invalid")

    sys.exit()





print("Valid")


answered by: Satyam Keshri
Add a comment
Know the answer?
Add Answer to:
Python coding
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
  • Python coding

    The Weight Detection SensorThe Gocargo carriers has planned to automate the filling of cargos in the container. They have installed a weight detection sensor in the conveyor belt where cargos will be taken to the container. The sensor needs to be programmed in a way that it stops the conveyor sending the cargos, when the maximum capacity of the container is reached or exceeded.Please write a code to enable the sensor to automate this task of finding the number of...

  • Python Coding

    The Cargo arrangementThe Gocargo carriers have a pattern of arranging the cargos in the container. Given the length of edge of the container, print the cargo arrangement pattern in the container as shown in the sample IO. The base of the container is always a square.Note: The Cargo are of unit length.Input format :     The input is an integer that corresponds to the side length of the container.Output Format:     Output is the cargo arrangement pattern inside the container.Please refer to the sample...

  • Python coding

    Palindromic CargoThe Gocargo carriers are celebrating their digitization process, which has given them a lot of benefits and ease of work for their employees. In account of this initiative they are offering their customers to ship their cargoes free of cost, if and only if the cargo names are palindromic.Given a cargo name, check whether the cargo name is a palindrome or not. Consider only alphabets while checking, remove special characters and spaces.Note : Define a function named check Palindrome()...

  • in c++ please RFIDs and lot Internet of things lloT) is one of the hottest technologies...

    in c++ please RFIDs and lot Internet of things lloT) is one of the hottest technologies at the moment. The concept of loT is to provide objects with unique identifiers (UIDs) and the ability to transfer their date over a network without requiring human interaction. Radio frequency identification system (RFID) is a technology that aids computers to identify objects and record their metadata By connecting RFID readers to the Internet, it will be able to send objects' date over the...

  • Problem #1 (8 pts) Suppose a textbook with 4 the book. What is the probability that a particular ...

    Problem #1 (8 pts) Suppose a textbook with 4 the book. What is the probability that a particular page has 50 pages has 200 misprints which is distributed randomly throughout 3 or more misprints? Problem #2 (8 pts): A box contains 8 large, 5 medium, and 3 small bolts and another box contains 6 nuts which fits the large bolts, 4 nuts which fits the medium bolts and 2 nuts which fits the small bolts. If one bolt and one...

  • Assume that during the past month, Fineway produced 10,000 cartons of highlighter Highlighters has a translucent...

    Assume that during the past month, Fineway produced 10,000 cartons of highlighter Highlighters has a translucent barrel and cap with a visible ink supply for see through colou The special fluorescent ink is fade and water resistance. Each carton contains 100 boxes markers and each box contains five markers. The markers come in boxes of one of fiv fluorescent colours- orange, blue, yellow, green and pink and in a five colour set. The standard cost for one carton of 500...

  • b. How many liters of oxygen gas must react with 0.800kg of Fesmo" atm? CHEAP 01 8. When ammonia gas, NH, reacts wi...

    b. How many liters of oxygen gas must react with 0.800kg of Fesmo" atm? CHEAP 01 8. When ammonia gas, NH, reacts with oxyoen, gaseous nitric Oxide (NO) and water vapor are collected. How many liters of NO can be made from 12L of oxygen and 10.0 of ammonia at constant pressure and temperature? 9. If a 1.0L container is filled with 1.00g of hydrogen gas and another 1.00L container is filled with 1.00g of nitrogen gas. Both containers are...

  • C++ Program Description Unloading Merchandise and Delivery (UMD) is in charge of loading air planes and...

    C++ Program Description Unloading Merchandise and Delivery (UMD) is in charge of loading air planes and trains from containers that have been unloaded from ships. The material from the dock is stacked (up to 5 containers high) if it to be sent by train. The materials destined to be sent by planes are are unpacked and placed on an assembly line. Each item is labeled either a train number or plane number (which is its destination). Items destined for trains...

  • Unloading Merchandise and Delivery (UMD) is in charge of loading air planes and trains from containers...

    Unloading Merchandise and Delivery (UMD) is in charge of loading air planes and trains from containers that have been unloaded from ships. The material from the dock is stacked (up to 5 containers high) if it to be sent by train. The materials destined to be sent by planes are are unpacked and placed on an assembly line. Each item is labeled either a train number or plane number (which is its destination). Items destined for trains are placed in...

  • CODING IN PYTHON: Assume a customer is at a store purchasing exactly 4 items. The cashier...

    CODING IN PYTHON: Assume a customer is at a store purchasing exactly 4 items. The cashier inputs the items and their prices into the computer. Someone has provided the cashier a Python program that does the following: 1. Asks cashier for name of each item (use short names like nails, paint, etc.) 2. Asks cashier for price of each item A) Assume items are between $5 & $20. Have 1 item that is less than $10 3. Prints receipt header...

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