Created
August 2, 2016 20:32
-
-
Save chevinbrown/bfbaf56ce2b4f7cc557c13648733cc13 to your computer and use it in GitHub Desktop.
Flatten Array of Integers
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 | |
# Flattens only an array of integers with varying dimensions | |
def flatty! | |
self.to_s.tr('[]','').split(',').map(&:to_i) | |
end | |
def flatty_test | |
a1 = [[1, 2, [3]], 4] | |
a2 = [1, [2, 3, 4], [[[5, [6]]]]] | |
puts a1.flatty!.to_s | |
puts a2.flatty!.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment