Created
August 12, 2016 20:59
-
-
Save mklbtz/d2c5abb0b5be5c980d4486595ade0256 to your computer and use it in GitHub Desktop.
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
# O(nk) | |
class Array | |
def unzip ary | |
return [] if ary.empty? | |
count = ary.first.count | |
(0...count).map { |i| | |
ary.map { |tuple| | |
raise 'inconsistent tuple size' if tuple.count != count | |
tuple[i] | |
} | |
} | |
end | |
end | |
zipped = [[:a, 1], [:b, 2]] | |
zipped.unzip | |
#=> [[a, b], [1, 2]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment