0

I've been stuck on this for a while. As an assignment I need to transpose this 2D array without using the built in transpose method, and without altering the function name / output. I feel like it should be way easier than I'm making it out to be...

class Image 

def transpose
     @array.each_with_index do |row, row_index|
      row.each_with_index do |column, col_index|
        @array[row_index] = @array[col_index]
      end
    end 
  end 
end

image_transpose = Image.new([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])

print "Image transpose"
puts

image_transpose.output_image

puts "-----" 

image_transpose.transpose
image_transpose.output_image

puts "-----"

EDIT: I love that I'm still receiving responses to this over 6 years later. Keep 'em coming!

1
  • 1
    Try using Array#zip. Commented Feb 18, 2018 at 17:29

3 Answers 3

3

Try below code:

class Image 

  def initialize(array)
    @array = array
  end

  def transpose
    _array = @array.dup
    @array = [].tap do |a|
      _array.size.times{|t| a << [] }
    end
    _array.each_with_index do |row, row_index|
      row.each_with_index do |column, col_index|
        @array[row_index][col_index] = _array[col_index][row_index]
      end
    end 
  end


end

image_transpose = Image.new([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
image_transpose.transpose
Sign up to request clarification or add additional context in comments.

4 Comments

Boom, that was it! So in this case we're basically duplicating the array and pushing it into an empty array, then swapping the rows and columns and sending it back to the original array right?
Array.new(size){ []} } - This is nice and more cleaner.
Unlike Ruby’s built-in transpose method, yours mutates @array. It would not be difficult to reorganize your calculations to avoid the mutation. Incidentally, why did you begin _array with an underscore?
It was unintentional. You can use any variable name here. If you go my way, It is not possible to swap elements with duplication of original array. a = [], a.object_id will be 70178478030540. b = a, b.object_id will be 70178478030540. (Same as a). This means it is referencing to same memory location. If I swap elements in iteration, it will not give desired output. Yes there are many other ways to do it as answered. (Using zip).
3

I suggest you replace the method transpose with the following.

def transpose
  @array.first.zip(*@array[1..-1])
end

The need for the (undefined) method output_input is not evident. You will also need an initialize method, of course, to assign a value to the instance variable @array.

I assume you are being asked to improve upon the implementation of the method transpose; otherwise there would be no reason for the stipulation that you cannot use Ruby's built-in transpose method.

1 Comment

This one was definitely more concise, but I think my teacher wanted us to implement everything within the defined transpose method. Regardless, thanks for your help!
0

Try(Using while loop):

def transpose(matrix)
  transpose = []
  i = 0

  while i < matrix.size
   transpose[i] = []
   j = 0
   while j < matrix.size
     transpose[i] << matrix[j][i]
     j += 1
   end
   i += 1
  end

  transpose
end

Obviously there will be need of initialize as per the question. Purpose of this code is, performing transpose without built-in method.

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.