Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Oftentimes, a game will have to decide how it will load players into a game. For instance, developers will have to decide whether to load

Oftentimes, a game will have to decide how it will load players into a game. For instance, developers will have to decide whether to load in players 1 by 1, or load a portion of each player at a time, or using another method. However, a game developer must ensure that the game does not start before a player finishes loading into a game (or the game crashes and results in a failed load), or else that will create an unfair advantage. One way to create a fair schedule is by using theround-robin method:

Given apositive integer k (called aquantum) and character names as iterable objects, return a list of how a game will load players based on the round-robin scheduling method. For instance, say we have the following player names as iterable objects in the order of

"Zai", ["n","o","l","y"], ("A","t","o","m","i","c"),and["M","a","r","c","_","b","y","_","8"]withk = 2, you should return a list in the following order:

'Z', 'a','n', 'o','A', 't','M', 'a','i','l', 'y','o', 'm','r', 'c','i', 'c','_', 'b', 'y', '_', '8'

A short explanation of this ordering is that you should cycle through each name and add the next 2 elements (because thequantumis 2 for this example) of each name into your list. You first insert into the list'Z' and'a'from the first name,'n'and 'o'from the second name,'A'and 't' from the third name, then'M'and 'a' from the fourth name.In the second cycle you insert'i'from the first name, but then insert'l'and 'y' from the second name, and so on. In the case that you finish a name (like what happened with"Zai"), move onto the next name. Ifk = 3, you would instead add the next 3 elements of each name and the returned list order would look like this:

'Z', 'a', 'i', 'n', 'o', 'l','A', 't', 'o','M', 'a', 'r','y','m', 'i', 'c','c', '_', 'b', 'y', '_', '8'

Hints:

  1. There are multiple ways to determine when to end the function and return the list. One way to do this is to keep track of how many times you need to add into the list, based on the number of elements in each of the names. Another way is to return when you reach a point where the length of output does not change, since that would indicate that all of the objects have been fully iterated through and there are no more elements that need to be added to the output.
  2. It may be useful to use iterators. We can typecast an object into an iterable object by usingiter(). This allows us to use thenext() function on that iterable object to obtain elements from the object one by one. This provides an easier alternative compared to indexing, since all we need to do is use next().
  3. Thenext() function takes an optional second argument (calleddefault), which specifies what value to return if the iterator reaches the end (has no more elements).

Requirements: Assert statements. You can use the provided utility function is_iterable in order to determine if something is an iterable object. You may use loops for this question.

defload_names(k, *names):

"""

>>> P1 = "Zai"

>>> P2 = ["n","o","l","y"]

>>> P3 = ("A","t","o","m","i","c")

>>> P4 = ["M","a","r","c","_","b","y","_","8"]

>>> load_names(2, P1, P2, P3, P4)

['Z', 'a', 'n', 'o', 'A', 't', 'M', 'a', 'i', 'l', 'y', 'o', 'm', 'r', \

'c', 'i', 'c', '_', 'b', 'y', '_', '8']

>>> load_names(3, P1, P2, P3, P4)

['Z', 'a', 'i', 'n', 'o', 'l', 'A', 't', 'o', 'M', 'a', 'r', 'y', 'm', \

'i', 'c', 'c', '_', 'b', 'y', '_', '8']

>>> P4 = ("G", "i", "n", "a", 1, 9, 9, 6)

>>> P5 = "Rae"

>>> P6 = ["T", "o", "a", "s", "t"]

>>> load_names(4, P4, P5, P6)

['G', 'i', 'n', 'a', 'R', 'a', 'e', 'T', 'o', 'a', 's', 1, 9, 9, 6, 't']

"""

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions