Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write python generators below: def shuffle(*args): pass The shuffle generator ta

ID: 3832762 • Letter: W

Question

Write python generators below:

def shuffle(*args):
pass

The shuffle 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. For example for i in shuffle ('abcde', 'fg', 'hijk'): print (i, end = '') prints afhbgicjdke.

Explanation / Answer

def shuffle(*args):
word = ""
iter_list = []
for arg in args:
iter_list.append(iter(arg))
while True:
word = ""
for it in iter_list:
try:
word += next(it)
except:
continue
if word == "":
break
yield word
  
for i in shuffle('abcde', 'fg', 'hijk'):
print(i, end='')
print("")

# code link: https://paste.ee/p/9jJUe

# I am enjoying these generator based question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote