Skip to content

Instantly share code, notes, and snippets.

@singingwolfboy
Created July 11, 2011 22:40
Show Gist options
  • Save singingwolfboy/1076968 to your computer and use it in GitHub Desktop.
Save singingwolfboy/1076968 to your computer and use it in GitHub Desktop.
Merging two sorted lists
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