Created
July 11, 2011 22:40
-
-
Save singingwolfboy/1076968 to your computer and use it in GitHub Desktop.
Merging two sorted lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge(lst1, lst2): | |
sorted = [] | |
iter1 = iter(lst1) | |
iter2 = iter(lst2) | |
l1_done = False | |
l2_done = False | |
val1 = lst1.next() | |
val2 = lst2.next() | |
while True: | |
if val1 < val2 or l2_done: | |
sorted.append(val1) | |
try: | |
val1 = lst1.next() | |
except StopIteration: | |
l1_done = True | |
else: | |
sorted.append(val2) | |
try: | |
val2 = lst2.next() | |
except StopIteration: | |
l2_done = True | |
if l1_done and l2_done: | |
return sorted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment