Working on this monkey patching problem and I am close to solving it, but can’t seem to assign my output without the nil values. I got the reassignment correct, but it is adding the values to the end of the new sub arrays rather than assigning them to the nil values.
def my_transpose
arr = Array.new(self.length) { Array.new(self.length) }
self.each_with_index do |el, i| # [0,0],[0,1],[0,2] => [0,0],[1,0],[2,0]
# [1,0],[1,1],[1,2] => [0,1],[1,1],[2,1]
el.each_with_index do |e, i| # [2,0],[2,1],[2,2] => [0,2],[1,2],[2,2]
p arr[i] << e
end
end
return arr
end
-
Array PART 2 #my_transpose should transpose a 2D array with square dimensions by returning a new 2D array where the
horizontal rows are converted to vertical columns
Failure/Error: expect(arr_1.my_transpose).to eq(expected_1)expected: [["a", "d", "g"], ["b", "e", "h"], ["c", "f", "i"]] got: [[nil, nil, nil, "a", "d", "g"], [nil, nil, nil, "b", "e", "h"], [nil, nil, nil, "c", "f", "i"]] (compared using ==) Diff: @@ -1,2 +1,4 @@ -[["a", "d", "g"], ["b", "e", "h"], ["c", "f", "i"]] +[[nil, nil, nil, "a", "d", "g"], + [nil, nil, nil, "b", "e", "h"], + [nil, nil, nil, "c", "f", "i"]]