Question

In: Computer Science

what is the solution of this code? Also how would I run it in python to...

what is the solution of this code? Also how would I run it in python to check?

q = Queue()
q.enqueue(10)
q.enqueue(12)
q.enqueue(15)
for i in range(len(q)):
if len(q) % 2 == 0:
→ → q.enqueue(q.dequeue())
else:
→ → q.dequeue()
print(q)

Solutions

Expert Solution

Queue is a datastructure that follows first in first out order insertion or deletions.

q.enque(10) // 10 is added to rear end of the queue. 10

q.enque(12) // 12 is added to rear end of the queue. 10 12

q.enque(15) // 15 is added to rear end of the queue. 10 12 15.

next we have a for loop running upto length of Queue. Here the length of the queue is 3.so for loop runs for 3 times here.

3% 2 ! = 0 , so the else block will execute. Here dequeue(), so 10 will be removed from the queue. So now queue becomes : 12 15

len(q) now becomes 2, and 2%2 == 0, so if block will execute. q.enqueue(q.dequeue()), so q.dequeue returns 12, so 12 will be removed and we are passing 12 to enqueue(), so 12 will be added to the end of the queue. So final queue is: 15 12.

len(q) now is again 2, so if block will again execute, so we dequeue the 15 and add 15 to queue again, so final queue is: 12 15.

so finally printing q will print [ 12 15] to the console.


Related Solutions

This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would be appreciated 8.19 LAB*: Program: Soccer team roster (Dictionaries) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers and the ratings in a...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
I cannot get this code to run on my python compiler. It gives me an expected...
I cannot get this code to run on my python compiler. It gives me an expected an indent block message. I do not know what is going on. #ask why this is now happenning. (Create employee description) class employee: def__init__(self, name, employee_id, department, title): self.name = name self.employee_id = employee_id self.department = department self.title = title def __str__(self): return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)    def main(): # Create employee list emp1...
I would like the following code in PYTHON please, there was a previous submission of this...
I would like the following code in PYTHON please, there was a previous submission of this question but I am trying to have the file save as a text file and not a json. Any help would be greatly appreciated. Create an application that does the following: The user will enter product codes and product numbers from the keyboard as strings. The product code must consist of four capital letters. The product number can only consist of numeric characters, but...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Hi, I would like the following python code rewritten in a different way/structure etc. I got...
Hi, I would like the following python code rewritten in a different way/structure etc. I got it from a question I posted but am worried fellow classmates will also be using it so am covering bases. her it is. #threeUniqueSongs.py #Simulation of professor listening to 3 songs out of playlist of 4 songs 10000 times sampling with replacement possible import random import math #Here playlist is the list of 4 songs i.e. "Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster,...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT