-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX10.26.py
37 lines (30 loc) · 887 Bytes
/
EX10.26.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
# 10.26 (Merge two sorted lists) Write the following function that merges two sorted lists
# into a new sorted list:
# def merge(list1, list2):
# Implement the function in a way that takes len(list1) + len(list2) comparisons.
# Write a test program that prompts the user to enter two sorted lists and
# displays the merged list.
def merge(l1, l2):
res = []
c1 = 0
c2 = 0
while c1 < len(l1) and c2 < len(l2):
m1 = l1[c1]
m2 = l2[c2]
if m1 < m2:
res.append(m1)
c1 += 1
else:
res.append(m2)
c2 += 1
while c1 < len(l1):
res.append(l1[c1])
c1 += 1
while c2 < len(l2):
res.append(l2[c2])
c2 += 1
return res
l1 = [int(x) for x in input("Enter list1: ").split()]
l2 = [int(x) for x in input("Enyer list2: ").split()]
res = merge(l1, l2)
print(res)