Skip to content

Instantly share code, notes, and snippets.

@iapain
Created January 18, 2019 12:08
Show Gist options
  • Save iapain/b60222e06d1c067de5a3c4ab48eeece8 to your computer and use it in GitHub Desktop.
Save iapain/b60222e06d1c067de5a3c4ab48eeece8 to your computer and use it in GitHub Desktop.
MergeSortedLists created by iapain - https://repl.it/@iapain/MergeSortedLists
pre_scores = [10, 20, 30, 40, 50, 60, 128, 150]
post_scores = [1, 30, 80, 100, 110, 115, 125, 126, 127, 128, 128]
# Merge sorted array in linear time
# O(n)
def merge_sorted_arrays(a, b):
i = 0
j = 0
while (i < len(a) and j < len(b)):
if (a[i] < b[j]):
yield a[i]
i += 1
elif (a[i] == b[j]):
yield a[i]
yield b[j]
i += 1
j += 1
else:
yield b[j]
j += 1
if (i < len(a)):
for x in range(i, len(a)): yield a[x]
if (j < len(b)):
for x in range (j, len(b)): yield b[x]
# Usage
print(list(merge_sorted_arrays(pre_scores, post_scores)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment