Created
November 8, 2016 08:33
-
-
Save phoenixwizard/73519ebd19ee7cef6267ce972483214c to your computer and use it in GitHub Desktop.
Array Flatten
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
require 'json' | |
def flatten_current_array(array,new_array) | |
if array.class == Array | |
array.each do |elem| | |
flatten_current_array(elem,new_array) | |
end | |
else | |
new_array.push(array) | |
end | |
end | |
begin | |
input_string = gets.strip | |
input_array=JSON.parse input_string | |
flattened_array=[] | |
flatten_current_array(input_array,flattened_array) | |
puts flattened_array.to_s | |
rescue Exception => e | |
puts "Error Message: #{e.message}" | |
puts "Error Trace: #{e.backtrace.inspect}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment