Blog

Define a function named stackToQueue. This function expects a stack as an argument.

1. Using the format of Table 15.2, complete a table that involves the following sequence of queue operations:

Label your answer columns using the following wording: State of the Queue After the Operation; Value Returned; and Comment.

2. Define a function named stackToQueue. This function expects a stack as an argument. The function builds and returns an instance of LinkedQueue that contains the elements in the stack. The function assumes that the stack has the interface described in Chapter 14. The function’s postconditions are that the stack is left in the same state as it was before the function was called, and that the queue’s front element is the one at the top of the stack.

Modify the supermarket checkout simulator so that it simulates a store with many checkout lines.

Modify the supermarket checkout simulator so that it simulates a store with many checkout lines. Add the number of cashiers as a new user input. At instantiation, the model should create a list of these cashiers. When a customer is generated, it should be sent to a cashier randomly chosen from the list of cashiers. On each tick of the abstract clock, each cashier should be told to serve its next customer. At the end of the simulation, the results for each cashier should be displayed.

Modify the simulation of Project 5 so that it takes account of the first factor.

1. In real life, customers do not choose a random cashier when they check out. They typically base their choice on at least the following two factors:

a The length of a line of customers waiting to check out.

b The physical proximity of a cashier.

Modify the simulation of Project 5 so that it takes account of the first factor.

2. Modify the simulation of Project 5 so it takes account of both factors listed in Project 6. You should assume that a customer initially arrives at the checkout line of a random cashier and then chooses a cashier who is no more than three lines away from this spot. This simulation should have at least four cashiers.

What is the running time of the __eq__ method, expressed in big-O notation?

1 Someone suggests that ArrayIndexedList should extend ArrayStack and LinkedIndexedList should extend LinkedQueue. Discuss the advantages and disadvantages of this proposal.

2 Define a method __eq__ for the ArrayIndexedList class. This method returns True if the two arguments are identical, or if they are of the same type, have the same number of items, and their items are equal at each position. Otherwise, the method returns False.

3 What is the running time of the __eq__ method, expressed in big-O notation?

Write a program that allows the user to reformat the text in a file.

Most word processors have a feature called word wrap, which automatically moves the user’s next word down a line when the right margin is reached. To explore how this feature works, write a program that allows the user to reformat the text in a file. The user should input the line width in characters and input the names of the input and output files. The program should then input the words from the file into a list of sublists. Each sublist represents a line of text to be output to the file. As the words are input into each sublist, the program tracks the length of that line to ensure that it is less than or equal to the user’s line length. When all the words have been….