In: Computer Science
Python question
Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval position. Every time you reach the end of the items, continue counting at the beginning of the remaining elements. Eventually, all elements will be removed. The function returns a list of elements in the order that they are removed. Use a queue object to implement this functionality.
To provide an illustrative example: assume that there are five items: 1, 2, 3, 4, 5 and that the interval is 3. The interval of 3 means that every third item is removed. So, the first item removed is 3, which leaves: 1, 2, 4, 5. Counting off from where 3 was—and continuing our count from the beginning when we reach the end of the items—the next item to remove is 1, which leaves: 2, 4, 5. The next item removed is 5, leaving: 2, 4. Then 2 is removed, followed by 4. So the selection order is: 3, 1, 5, 2, 4.
Note 1: You can assume that the Queue class is
given and that the is_empty(),
enqueue(), dequeue(),
peek() and size()
methods are available.
Hint: Remember that queues maintain the order of
elements added to them, so if you had all items in a queue then you
could cycle through each of them (potentially infinitely) by
getting the next item with a dequeue()
and reinserting it back into the queue using the
enqueue() method.
For example:
Test | Result |
---|---|
item_collection = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(selection_order(item_collection,2)) |
[2, 4, 6, 8, 1, 5, 9, 7, 3] |
item_collection = ["Hoiho", "Huia", "Kaka", "Kakapo", "Kaki"] print(selection_order(item_collection,3)) |
['Kaka', 'Hoiho', 'Kaki', 'Huia', 'Kakapo'] |
item_collection = ["Karearea", "Kea", "Kiwi", "Kotare", "Kotuku", "Matata"] print(selection_order(item_collection,4)) |
['Kotare', 'Kea', 'Karearea', 'Kiwi', 'Matata', 'Kotuku'] |
ANSWER:
I have provided the properly commented
and indented code so you can easily copy the code as well as check
for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
# sample class queue as not provided in question
class Queue:
# defining constructor to class
# default parmeter is empty list
def __init__(self,data=[]):
# initializing queue
self.queue = data
# defining size of data
self.size = len(data)
# defining enqueue method
def enqueue(self,item):
# appending item to queue
self.queue.append(item)
# increment size
self.size += 1
# defining dequeue method
def dequeue(self):
# if list is empty returning None
if self.is_empty():
return None
# decrementing size
self.size -= 1
# else poping first element and returning it
return self.queue.pop(0)
# defining peek method
def peek(self):
# if list is empty returning None
if self.is_empty():
return None
# else returning first element
return self.queue[0]
# defining size method in list
def size(self):
return self.size
# is_empty method
def is_empty(self):
return self.size==0
# defining function selection_order
def selection_order(item_collection,interval):
# defining Queue object with list of item_collection
queue = Queue(item_collection)
# defining a empty list to store result elements
result= []
# looping till queue is not empty
while not queue.is_empty():
# looping dequeue and enqueue element till interval-1 to get the
# element at iterval to head node and finally storing the element to result list
for i in range(interval-1):
# dequeue element from list
item = queue.dequeue()
# enqueue back to queue
queue.enqueue(item)
# reached till element at interval, storing to result list
item= queue.dequeue()
result.append(item)
# returning the item list
return result
# Testing
item_collection = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(selection_order(item_collection,2))
item_collection = ["Hoiho", "Huia", "Kaka", "Kakapo", "Kaki"]
print(selection_order(item_collection,3))
item_collection = ["Karearea", "Kea", "Kiwi", "Kotare", "Kotuku", "Matata"]
print(selection_order(item_collection,4))
OUTPUT IMAGE