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

This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT