0

I need to create a multidimensional array from an array.

For example, let's say the initial array = [1,2,3,4,5,6]

I need a multidimensional array of

[ [1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5],[1,2,3,4,5,6] ]

I feel like this should be so easy, but I am stuck.

Here's what I have so far, which is wrong

def solution(a)
  empty =[]
  a.each do |x|
    new_array = Array(x)
    empty.push(new_array)   
  end
  empty.reverse
end

and I've tried

def solution(a)
  empty =[]
  for i in 1..a.size
    new_array = Array(a.pop)
    empty.push(new_array)   
  end
  empty.reverse
end

Anybody have a solution or suggestion?

EDIT: I realized that I never specified whether the array will consist of more than integers. For my purposes, I am looking for a solution that will accommodate integers or strings.

4 Answers 4

2

You could use flatten:

arr = []
arr = [1,2,3,4,5,6].map{ |elm| arr = [arr, elm].flatten }
arr # => [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6]]
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you want to generate lists of sequential numbers, use 1.upto(6) to iterate 6 times, then make the individual arrays by mapping with 1.upto(i).

1.upto(6).map { |i| 1.upto(i) }

This makes an Enumerator of Enumerators. These should be fine, and you'll save memory if they get large. If you want to force them to be Arrays, add to_a.

1.upto(6).map { |i| 1.upto(i).to_a }

If you want a more generic solution, use Cary's answer.

4 Comments

Despite the question stating, "let's say the initial array = [1,2,3,4,5,6]" your solution is unrelated to the value of array; it's just a shorthand way of writing the literal [[1[, [1,2],...]. I assume array could be [1,2,3,4,5] or ['cat', 7, :dog, 3.1], and if it were the latter, the OP would want [["cat"], ["cat", 7], ["cat", 7, :dog], ["cat", 7, :dog, 3.1]]. Other answers interpreted the question similarly. David, please clarify: is the specific array [1,2,3,4,5,6] the only one of interest or do you want the array to be arbitrary in size and content, or something else?
@CarySwoveland I took that to be part of the solution, not the problem. If so, replace 1.upto(6) with the initial array.
Let's see what David says.
Ah this is a good point. For my purposes, I will actually be dealing with non-integers. I just used integers since it was the simplest way to write my question down. I replaced 1.upto(6) with the original array, but I am realizing I should mark another answer as the solution. So sorry about the lack of clarity.
1
arr = [1,2,3,4,5,6]

arr.size.times.map { |i| arr[0..i] }
  #=> [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5],
  #    [1, 2, 3, 4, 5, 6]] 

Comments

0

The question is not quite accurate so in more general way:

def multidemensional(ary)
  ary.map { |x| 1.upto(x).to_a }
end
multidemensional([1, 2, 3, 4, 5, 6])
# => [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6]]

multidemensional([2, 2, 3, 7])
# => [[1, 2], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5, 6, 7]]

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.