Last active
December 10, 2015 03:08
-
-
Save mpenkov/4372560 to your computer and use it in GitHub Desktop.
Coding Interview Practice: MergeSort merge step
This file contains hidden or 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
# | |
# lessons learnt: | |
# | |
# - python's list has no find() method -- the correct name is index() | |
# - next() is a built-in Python function -- http://stackoverflow.com/questions/1733004/python-next-function | |
# | |
def merge(arrays): | |
result = list() | |
while arrays: | |
heads = [a[0] for a in arrays] | |
next_value = min(heads) | |
arrays[heads.index(next_value)].remove(next_value) | |
result.append(next_value) | |
arrays = filter(None, arrays) | |
return result | |
if __name__ == "__main__": | |
arrays = [[0, 5, 8, 9], [1, 2, 7], [10]] | |
print merge(arrays) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment