188k views
5 votes
he alternate_all generator takes any number of iterables as parameters: it produces the first value from the first parameter, then the first value from the second parameter, ..., then the first value from the last parameter; then the second value from the first parameter, then the second value from the second parameter, ..., then the second value from the last parameter; etc. If any iterable produces no more values, it is ignored. Eventually, this generator produces every value in each iterable. Hint: I used explicit calls to ite

1 Answer

6 votes

Answer:

See Explaination

Step-by-step explanation:

If we do for this sequence :-

for i in alternate_all('abcde','fg','hijk'):

print(i,end='')

The code can be given as :-

def alternate_all(*args):

itrs = [iter(arg) for arg in args]

while itrs != []:

temp = []

for i in range(len(itrs)):

try:

yield next(itrs[i])

temp.append(itrs[i])

except StopIteration:

pass

itrs = temp

The above python code will generate the output as the first value from the first parameter, then the first value from the the second parameter, then the first value from the last parameter and do on.

User Raceimaztion
by
3.4k points