Skip to content

Instantly share code, notes, and snippets.

@mpenkov
Last active December 10, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpenkov/4372560 to your computer and use it in GitHub Desktop.
Save mpenkov/4372560 to your computer and use it in GitHub Desktop.
Coding Interview Practice: MergeSort merge step
#
# 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