-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterleavingLists.py
More file actions
52 lines (40 loc) · 1.44 KB
/
InterleavingLists.py
File metadata and controls
52 lines (40 loc) · 1.44 KB
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
41
42
43
44
45
46
47
48
49
50
## Author: Jose F. Martinez Rivera
## Student Number: 802-10-4088
## Course: ICOM4036 - 040
## Professor: Dr. Wilson Rivera
## Hand-In Date: March 12, 2013
#2. Write a program that implements two functions:
# AllInter(a,b) and ParallelInter(a,b) where a and b
# are two lists and the function AllInter returns a list
# containing all the interleaving, while ParallelInter returns
# the parallel interleaving. The two input lists may or may not
# be of equal length. For example:
#Interleaves two lists in a parallel manner
#>> ParallelInter( [1, 2], [3, 4])
#[1, 3, 2, 4]
def ParallelInter(a,b):
if not a:
return b
if not b:
return a
else:
return [a[0], b[0]] + ParallelInter(a[1:len(a)],b[1:len(b)])
#All possible interleavings of two lists
# >>AllImter([1, 2], [3, 4])
#[[1,2,3,4], [1,3,2,4], [1,3,4,2], [3,1,2,4], [3,1,4,2], [3,4,1,2]]
def AllInter(a,b):
if not a:
return [b]
if not b:
return [a]
else:
xList = AllInter(a[1::], b[0::])
xList = prepend(a[0], xList)
yList = AllInter(a[0::], b[1::])
yList = prepend(b[0], yList)
return xList + yList
#Used for AllInter, it prepends an element to a list
def prepend(a, aList):
for i in range(0,len(aList)):
aList[i].insert(0,a)
return aList