Created
May 5, 2012 11:21
-
-
Save israelst/2601661 to your computer and use it in GitHub Desktop.
Merging two ordered 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(x, y): | |
if len(x) == 0: | |
return y | |
if len(y) == 0: | |
return x | |
last = y.pop() if x[-1] < y[-1] else x.pop() | |
# 'merged' is required because the append method is in place | |
merged = merge(x, y) | |
merged.append(last) | |
return merged |
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(x, y): | |
if len(x) == 0: | |
return y | |
if len(y) == 0: | |
return x | |
if x[0] <= y[0]: | |
return [x[0]] + merge(x[1:], y) | |
else: | |
return [y[0]] + merge(x, y[1:]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adicionei uma solução sem slice e concat.