takes two lists that are already sorted, and merges them together.
another simple solution but with higher order (O) is to simply sorted(a + b)
the two lists.
def merge_lists(a, b):
"""
takes two sorted lists, and merges them
"""
sorted_list = []
# grab inital values
a_i = 0
b_i = 0
# while lists are not empty
while len(a) != a_i and len(b) != b_i:
if a[a_i] < b[b_i]:
sorted_list.append(a[a_i])
a_i += 1
else:
sorted_list.append(b[b_i])
b_i += 1
# append whatever is remaining
[sorted_list.append(i) for i in a[a_i:]]
[sorted_list.append(i) for i in b[b_i:]]
return sorted_list
import codewars_test as test
# TODO Write tests
from solution import merge_lists# or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
a = [1,1,3,5,8,10]
b = [2,4,5,5,9]
c=[1, 1, 2, 3, 4, 5, 5, 5, 8, 9, 10]
test.assert_equals(merge_lists(a,b),c)
@test.it("test empty list")
def test_case2():
a = [1,1,3,5,8,10]
b = []
c = sorted(a+b)
test.assert_equals(merge_lists(a,b),c)