forked from comp-think/comp-think.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
58 lines (48 loc) · 2.17 KB
/
merge.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
# Copyright (c) 2019, Silvio Peroni <essepuntato@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
# Test case for the algorithm
def test_merge(left_list, right_list, expected):
result = merge(left_list, right_list)
if expected == result:
return True
else:
return False
# Code of the algorithm
def merge(left_list, right_list):
result = list()
# Repeat until both lists have at least one item
while len(left_list) > 0 and len(right_list) > 0:
left_item = left_list[0]
right_item = right_list[0]
# If the item in left_list is less than the one in right_list,
# add the formed to the result and remove it from left_list
if left_item < right_item:
result.append(left_item)
left_list.remove(left_item)
# Otherwise, the item in right_list is added to the result and
# removed from the right_list
else:
result.append(right_item)
right_list.remove(right_item)
# Add to the result the remaining items from the lists
result.extend(left_list)
result.extend(right_list)
return result
print(test_merge([], [], []))
print(test_merge([1, 2, 2, 3], [], [1, 2, 2, 3]))
print(test_merge([1, 2, 2, 3], [2, 3, 7], [1, 2, 2, 2, 3, 3, 7]))
print(test_merge(["Coraline", "Good Omens", "The Graveyard Book"],
["American Gods", "Neverwhere"],
["American Gods", "Coraline", "Good Omens", "Neverwhere", "The Graveyard Book"]))