Question

Questions Submission Question 2: Stacks Problem Write a program that keeps track of where cars are...

Questions


Submission Question 2: Stacks

Problem
Write a program that keeps track of where cars are located in a parking garage to estimate the time to retrieve a car when the garage is full.
This program will demonstrate the following:

How to create a list for use as a stack,


How to enter and change data in a stack.


Solving the Problem
Step 1
In Python, the stack data structure is actually implemented using the list data structure with a few additional functions. These functions come as part of the list structure, so it is really more about how to access the list to make it function like a stack rather than it actually being a different data structure from the list.
Step 2
To add cars to the stack, use the append method of the list structure like you would with any normal list.
Step 3
To find how long it will take to retrieve a certain car, you will have to use the pop() function of the list until you find the required vehicle in the garage stack and count the number of vehicles.
Step 4
Once the number of cars in front of the vehicle are found, the time taken to retrieve the car can be calculated based on the average time it takes to move a vehicle plus the time to retrieve the car.
Step 5
Finally, the estimated time to retrieve the vehicle can be output to the user to inform the car owner.
Documentation Guidelines:
Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the '<' and '>' angle brackets are only placeholders.  You should replace the placeholders and the comments between them with your specific information).  Your cover sheet should have some of the same information, but what follows should be at the top of each program's sheet of source code.  Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose.  Be brief and to the point.  Start your design by writing comment lines of pseudocode.  Once that is complete, begin adding executable lines.  Finally run and test your program.

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

Note: The code has been implemented in Python 3. Please indent the code as shown in the screenshots.

Program screenshots:

Sample output:

Code to copy:

# Define the main() function.

def main():

    # Declare the required variables.

    stack_cars = []

    move_time = 1

    retrieve_time = 2

    # Start the loop to display the menu.

    while True:

        # Display the choices to the user.

        print("\n\t\t\t Car Stack Menu")

        print("\n1. Add a car")

        print("2. Retrieve a car")

        print("3. Show the car stack")

        print("4. Exit")

        # Prompt the user to enter the input.

        choice = input("\nEnter your choice: ")

        # Append the car in the stack if the choice is 1.

        if choice == '1':

            car = input("Enter the car name to add: ")

            stack_cars.append(car)

        

        # Remove the car from the stack if the choice is 2.

        elif choice == '2':

            car = input("Enter the car name to retrieve: ")

            # Display the error message if the car is not present.

            if car not in stack_cars:

                print(car + " is not present in the stack.")

            # Otherwise, compute the time to retrieve a car.

            else:

                # Declare a temp stack to move the cars.

                temp = []

                x = stack_cars.pop()

                count = 0

                # Start the loop to pop the cars from the stack

                # until the car to be retrieved is found.

                while x != car:

                    count += 1

                    # Store the car in the temp stack.

                    temp.append(x)

                    x = stack_cars.pop()

                # Start the loop to move the cars back in the

                # original stack.

                while len(temp) != 0:

                    stack_cars.append(temp.pop())

                # Compute and display the total time.

                total_time = move_time * count + retrieve_time

                print("Total time to retrieve", car, "=", total_time)

        

        # Display the stack if the choice is 3.

        elif choice == '3':

            print("Car stack =", stack_cars)

        # Return from the funtion if the choice is 4.

        elif choice == '4':

            print("Exiting from the program...")

            return

        

        # Display the message for invalid input.

        else:

            print("Error: Invalid choice!")

# Call the main() function.

if __name__ == "__main__":

    main()

Add a comment
Know the answer?
Add Answer to:
Questions Submission Question 2: Stacks Problem Write a program that keeps track of where cars are...
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
  • Problem You work for a dealership that deals in all kinds of vehicles. Cars, Trucks, Boats,...

    Problem You work for a dealership that deals in all kinds of vehicles. Cars, Trucks, Boats, and so forth need to be inventoried in the system. The inventory must be detailed so that it could be searched based on number of doors, engine type, color, and so on. This program will demonstrate the following: How to create a base class How to extend a based class to create new classes How to use derived classes Solving the Problem Step 1...

  • Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

    Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...

  • Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

    Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...

  • The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There...

    The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrace/exit to the garage at one end of the lanes. If a customer arrives to pick up a car which is not nearest the exit, all cars blocking the car's path are moved into the other lane. If more cars still must be moved out of the way, they go into the street. When the customer's car is driven out,...

  • Written in C++ language Write a program that keeps track of a telephone directory. The program...

    Written in C++ language Write a program that keeps track of a telephone directory. The program should create a structure, Person, with the following data: struct Person { string firstName; string lastName; string phoneNumber; }; The program should then create a vector of Person structs, called directory. Your program should have functions to: - Read the vector data from a data file “telephoneData.txt” into the vector directory. - Print the details of all Persons in the vector. You may use...

  • A liter is 0.264179 gallons. Write a program that will read in the number of liters...

    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so...

    ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so one stack instance):  aStack. Ask the user for 5 ints and push them onto aStack. Find a way to print the values in aStack in from the bottom to the top (so display the top LAST) - so if aStack was 1,2,3,4,5 with 5 at the top and 1 at the bottom you want it to display 1,2,3,4,5 (you don't want it to display 5,4,3,2,1...

  • Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e....

    Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e. your name, then try to compute this string in reverse order using a Stack structure with the Linked List principle. For example: "Nguyen Van A""A Nav Neyugn Exercise 2: 8 points Assume that a queue of N passengers waiting to enter a train car Only the first person in the queue can enter at a time New persons arrive will go to the end...

  • Question: How can I solve it in "Java program"?, i need test code also. Define Pi...

    Question: How can I solve it in "Java program"?, i need test code also. Define Pi to be a parking lane i in a parking lot and carj to be the jth car; where the number of lanes in a parking lot is 4 and there are 7 cars to park. 1<= i<=4, and Cj: 1<=j<=7. Assume that all lanes are of the same length, and all cars are also of the same length, hence, the same number of cars...

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