Question

In: Computer Science

Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...

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']

Solutions

Expert Solution

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


Related Solutions

In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return the linked list unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly and then return the linked list with the modifications, not a list. If you have a linked list 11 -> 12 -> 18 -> 20...
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return head unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly. If you have a linked list 11 -> 12 -> 18 -> 20 -> 24 -> 32 -> 38 -> 44 and skip =2, then you should...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
IN PYTHON Create a function called biochild.  The function has as parameters the number m...
IN PYTHON Create a function called biochild.  The function has as parameters the number m and the lists biomother and biofather.  The biomother and biofather lists contain 0’s and 1’s.  For example: biomother = [1,0,0,1,0,1] and biofather = [1,1,1,0,0,1]  Both lists have the same length n.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child).  The child...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT