2

So I have a basic problem that needs to be solved. I need to take the following nested array [1, 2, ["str", 1], [[2]]] and return [1,2,"str",1,2]. Ultimately removing all nested arrays. The following is my incomplete solution:

test = [1, 2, ["str", 1], [[2]]]

def remove_array(array)
    new_array = []
    array.each do |item|
        if item != Array
            puts "you are here"
            new_array << item
        else
            remove_array(item)
        end
    end
end

remove_array(test)

This is a classic recursion method. However, I cant seem to think of how to maintain new_array. Every time I pass the item that is an array to it, new_array doesn't maintain its state. I would also like to point out that I am aware of the built in method flatten as I am trying to solve the solution without said method. Any advice would be greatly appreciated.

1
  • are you trying to implement from scratch, or will a simple answer suffice? Commented Oct 27, 2015 at 1:54

5 Answers 5

4
def my_flatten(arr)
  result = []

  arr.each do |el|
    if el.is_a?(Array)
      result.concat(my_flatten(el))
    else
      result << el
    end
  end

  result
end

You generally had the right idea, but you were never adding the inner array elements into your result.

Sign up to request clarification or add additional context in comments.

Comments

2
def remove_array(array)
  new_array = []
  array.each do |e|
    if e.is_a?(Array)
      new_array += remove_array(e)
    else
      new_array << e
    end
  end
  new_array
end

3 Comments

I understand what += translates to, but I dont get how new_array += remove_array(e) preserves the state of new_array. Could you explain?
What do you mean by preserve the state of new_array? If array is [1, [2, 3]] and e is [2, 3], then at this point new_array is [1]. remove_array(e) returns [2, 3], so new_array += [2, 3] appends [2, 3] to [1], thus new_array becomes [1, 2, 3]. += is basically the same as Array#concat.
The way you explained it makes sense on how the the result is retrieved. Thank you.
0

You can call flatten on you array.

a = [1, 2, ["str", 1], [[2]]]
=> [1, 2, ["str", 1], [[2]]]
   a.flatten
=> [1, 2, "str", 1, 2]

Comments

0

In Ruby, there is a convenient built-in method called flatten

It will "flatten" your array so that all the inside arrays disappear, and it returns one array with all elements inside

Edit:

Sorry editing this. Forgot that you were using recursion for your solution. Since you're essentially creating a new array each recursion, I'm not exactly sure how I would fix your solution at this moment.

Comments

0

just use the flatten! :) keep it simple..

test = [1, 2, ["str", 1], [[2]]]

def remove_array(array)
   array.flatten!
   return array
end

remove_array(test)

2.2.1 :024 >   remove_array(test)
=> [1, 2, "str", 1, 2]

I hope this helps everybody! Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.