Last active
December 10, 2015 02:48
-
-
Save sczizzo/4369956 to your computer and use it in GitHub Desktop.
Coding Interview Practice (Dec. 24)
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
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