-
Notifications
You must be signed in to change notification settings - Fork 0
/
interleave_lists.py
40 lines (33 loc) · 1.21 KB
/
interleave_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
4. Interleave Lists
Write a Python function interleave(alist1, alist2) that,
given two lists with the same structure but not necessarily the same length,
alist1 and alist2, which may contain other lists,
returns a list with the interleaved elements of both alist1 and alist2.
For example:
● interleave([1, [4,2]], [3, [7,4]])
returns the list [1,3,4,7,2,4]
● interleave(['a','b','c'], [1,2,3,4,5])
returns the list ['a',1,'b',2,'c',3]
● interleave([], [1,2])
returns the list []
@author: Luísa Maria
"""
def interleave(alist1, alist2):
result = []
if(len(alist1) <= len(alist2)):
for i in range(0, len(alist1)):
if(type(alist1[i]) == list):
result = result + interleave(alist1[i], alist2[i])
else:
result.append(alist1[i])
result.append(alist2[i])
return result
else:
for i in range(0, len(alist2)):
if(type(alist2[i]) == list):
result = result + interleave(alist1[i], alist2[i])
else:
result.append(alist1[i])
result.append(alist2[i])
return result