Skip to content

Instantly share code, notes, and snippets.

@sczizzo
Last active December 10, 2015 02:48
Show Gist options
  • Save sczizzo/4369956 to your computer and use it in GitHub Desktop.
Save sczizzo/4369956 to your computer and use it in GitHub Desktop.
Coding Interview Practice (Dec. 24)
class Array
def merge_with other
res = []
until self.empty? or other.empty?
a, b = self.shift, other.shift
if a == b
res << a
elsif a < b
res << a
other.unshift b
else # a > b
res << b
self.unshift a
end
end
res += self + other
end
def merge ; self.reduce :merge_with end
end
puts [[0, 5, 8, 9], [1, 2, 7], [10]].merge.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment