python - Alternating Generator -


my professor wants write generator takes *args argument, , yields 1st value of 1st argument, 1st value of 2nd, , 1st of 3rd. , when that's done yields 2nd value of 1st, 2nd value of 2nd, etc.

i'm having bit of trouble iterating through iterables because they're of different lengths. while 1 iterable may have length of 4, other might have length of 2, , trying yield 3rd value of both results in error.

i want generator stop moment runs argument has run out of values iterate over.

edit: have far ...

temp = list(args) while true:     x in range(len(temp)):         letters in args:             yield next(letters) 

right gives me type error: 'str' object not iterator when try running line ...

[print(i) in alternate('abcde', 'fg', 'hijk')] 

i'm not allowed use zip or other imports.

the function can accept strings , iterables parameter

i ended solving problem (without use of zip since wasn't allowed to).

temp = [iter(arg) arg in args] while true:     x in range(len(temp)):         letters in temp:             yield next(letters) 

i used list comprehension create list of arguments while appending of them iterables. afterwards, used while loop while iterating through every value in list repeatedly until ran out of iterations. once detected there no more iterate from, broke out of loop.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -