Search This Blog

Thursday, September 24, 2020

NPTEL Assignment Solution for Data structures and algorithm using Python

Write a function shuffle(l1,l2) that takes as input two lists, 11 and l2, and returns a list consisting of the first elment in l1, then the first element in l2, then the second element in l2, then the second element in l2, and so on. If the two lists are not of equal length, the remaining elements of the longer list are appended at the end of the shuffled output.

def shuffle(list_a,list_b):
    common_num = []
    for a in range(max(len(list_a),len(list_b))):
      try:
        common_num.append(list_a[a])
      except IndexError:
                pass
      for b in range(a,a+1):
        try:
          common_num.append(list_b[b])
        except IndexError:
          break
    return common_num

No comments:

Post a Comment