Question: ( In Python ) Given an integer k and a queue of items. Write a program to reverse the order of the first k elements

(In Python)Given an integer k and a queue of items. Write a program to reverse the order of the first k elements of the queue. The other elements will be in the same relative order.
The following standard operations are allowed on the queue:
enqueue(item):insert an item to the rear of queue
dequeue(): remove an item from the front of queue, and return the value
size(): return the size of queue
is_empty(): return true if the queue has no elements
# Example 1
Input: Q =[1,2,3,4,5]
k =5
Output: Q =[5,4,3,2,1]
Explanation: The first 5 elements of Q are reversed.
# Example 2
Input: Q =[1,2,3,4,5,6]
k =4
Output: Q =[4,3,2,1,5,6]
Explanation: The first 4 elements of Q are reversed. The rest elements remains the same order
Task1(20 pts): Create and define class of Queue and Stack( Create both Queue.py and Stack.py)
Task2(5 pts): Import both Queue and Stack prpperly into main.py
Task3(25pts): Modify the following code and insert the missing parts to fullfill the program
Solution:
step1. Create and implement class Queue in a seperate Queue.py file under the main.py
step2. You will also need to create stack objects when completing the task, create or upload the Stack.py file as well
step3. define a function named reverse_kth(item_list, k) in ** main.py ** which requires two input parameters, the item_list will hold a sequence of nums or items, k is an integer which determines how many elements in the list to be reversed, use the following test cases to verify your program
print(reverse_kth([11,12,13,14,15,16,17],3))
print(reverse_kth(["One", "Two", "Three", "Four"],4))
print(reverse_kth([1,2,3,4,5,6],4))
print(reverse_kth([1,2,3,4,5,6],8))
# Sample output of above print statements:
13,12,11,14,15,16,17
Four,Three,Two,One
4,3,2,1,5,6
Error:The second input k is too big, should be less than or equal with the size of your Queue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!