-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergepostfix.py
More file actions
38 lines (32 loc) · 833 Bytes
/
mergepostfix.py
File metadata and controls
38 lines (32 loc) · 833 Bytes
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
import sys
def merge_sort(arr, left, right):
def merge(str1, str2):
result = ""
i = 1
j = 1
while i <= len(str1) and j <= len(str2):
if str1[-i] == str2[-j]:
result += str1[-i]
else:
break
i += 1
j += 1
return result[::-1]
if left < right:
mid = left + (right - left) // 2
str1 = merge_sort(arr, left, mid)
str2 = merge_sort(arr, mid + 1, right)
arr = merge(str1, str2)
elif left == right:
arr = arr[left]
return arr
words = str(sys.argv[1]).split(" ")
l = len(words)
result = merge_sort(words, 0, l - 1)
if len(result):
print(result)
else:
print(" ")
#python3 mergepostfix.py "starlink hyperlink weblink"
#python3 mergepostfix.py "starlink hyperlink weblink eink ink"
#python3 mergepostfix.py "starlink hyperlink weblink bing"