Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USE PYTHON3; DO NOT IMPORT PACKAGES if you find the below function helpful, you may use it within the probel Given a positive integer k
USE PYTHON3; DO NOT IMPORT PACKAGES
if you find the below function helpful, you may use it within the probel
Given a positive integer k and three iterable objects, write a generator that, in each round, yields the next k elements from each iterable object in order, and repeats such rounds. (This algorithm is also known as Round Robin Scheduling.) Note that the generator yields None if the iterable object cannot be iterated upon anymore. For example, when k is 4, but the iterator object only has 2 elements left, the iterator will yield None twice after yielding the remaining elements to fill in this gap. Example: >>> argi = "abcdefgh" >>> arg2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> arg3 = (True, False, True, False, True, False) >>> gen = round_robin_generator(2, arg1, arg2, arg3) If we keep calling next(gen), the output values will be: 'a', 'b', 1, 2, True, False, 'c','d', 3, 4, True, False, 'e', 'f', 5, 6, True, False, 'g', 'h', 7, 8, None, None, ... In the first round, the function outputs two elements from each iterator ('a' and 'b' from the first iterator, 1 and 2 from the second iterator, and True and False from the third iterator). The function repeats such rounds twice. In the fourth round, when the function tries to output the next two elements from the third iterator, it fails because all six elements from the third iterator have been output in the first three rounds, so it yields None to fill in this gap. Hints: (1) An infinite loop ("while True") may help to repeat our rounds indefinitely. (2) The next() function takes an optional second argument (called default), which specifies what value to return if the iterator reaches the end (has no more elements). def round_robin_generator (k, argi, arg2, arg3): >>> argi = "abcdefgh" >>> arg2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> arg3 = (True, False, True, False, True, False) >>> gen = round_robin_generator(2, argi, arg2, arg3) >>> [next(gen, None) for - in range (14)] ['a', 'b', 1, 2, True, False, 'c','d', 3, 4, True, False, I 'e','f'] >>> gen = round_robin_generator (3, argi, arg2, arg3) >>> [next(gen, None) for in range(14)] ['a', 'b', 'c', 1, 2, 3, True, False, True, 'd', 'e', 'f', 4, 5] >>> arg4 = 'dsc" >>> arg5 [2, 0] >>> argo = "fall" >>> gen = round_robin_generator (4, arg4, arg5, argo) >>> [next(gen, None) for in range (10)] ['d', 's', 'c', None, 2, 0, None, None, 'f', 'a'] # YOUR CODE GOES HERE # def is_iterable(obj): A function that checks if obj is a iterable (can be iterated over in a for-loop). DO NOT MODIFY THIS FUNCTION. You don't need to add new doctests for this function. >>> is_iterable(1) False >>> is_iterable("DSC_20") True >>> is_iterable(["Fall", 2020]) True try: iter(obj) return True except TypeError: return falseStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started