Ruby Argument Error, trying to transpose a matrix 90 degrees

59 Views Asked by At

In the below code I am trying to transpose a matrix 90 degrees. But when I run the code I get an argument error:

I don't understand why this is happening as I clearly have an argument in the method definition. I would appreciate any input on why this error is occurring. Thanks.

irb(main):001:0> matrix1 = [
irb(main):002:1*   [1, 5, 8],
irb(main):003:1*   [4, 7, 2],
irb(main):004:1*   [3, 9, 6]
irb(main):005:1> ]
=> [[1, 5, 8], [4, 7, 2], [3, 9, 6]]
irb(main):006:0> def rotate90(matrix_input)
irb(main):007:1>   input_column = 0
irb(main):008:1>   input_row = 0
irb(main):009:1>   return_column = matrix_input.size - 1
irb(main):010:1>   matrix2 = []
irb(main):011:1>   mat1_size = matrix_input.size -1 
irb(main):012:1>   loop do
irb(main):013:2*     loop do
irb(main):014:3*       matrix2[input_column][return_column] = matrix_input[input_row][input_column]
irb(main):015:3>       input_column += 1
irb(main):016:3>       break if input_column >= matrix_input[0].size - 1
irb(main):017:3>     end
irb(main):018:2>     input_row += 1
irb(main):019:2>     input_column = 0
irb(main):020:2>     return_column -= 1
irb(main):021:2>     break if input_row >= mat1_size
irb(main):022:2>   end
irb(main):023:1>   matrix2
irb(main):024:1> end
=> :rotate90
irb(main):025:0> rotate90(matrix1)
ArgumentError: wrong number of arguments (given 1, expected 0)
    from (irb):11:in `length'
    from (irb):11:in `rotate90'
    from (irb):25
    from /usr/bin/irb:11:in `<main>'
2

There are 2 best solutions below

1
On

If I correctly understand what you are trying to accomplish, it can be done as follows.

def rotate90(arr)
  arr.map(&:reverse).transpose
end

arr = [[1, 5, 8],
       [4, 7, 2],
       [3, 9, 6]]

rotate90(arr)
  #=> [[8, 2, 6],
  #    [5, 7, 9],
  #    [1, 4, 3]]

Note that

arr.map(&:reverse)
  #=> [[8, 5, 1],
  #    [2, 7, 4],
  #    [6, 9, 3]] 

arr.map(&:reverse) is roughly shorthand for

arr.map { |a| a.reverse }

Note also that

4.times.reduce(arr) { |a,_| rotate90(a) }
  #=> [[1, 5, 8],
  #    [4, 7, 2],
  #    [3, 9, 6]] 

returns the original array.

I see from the comment that the OP forgot to mention that neither Array#transpose or Array#reverse can be used. In that case we can take a different approach.

Observe that if arr_new is the array being constructed, then:

arr_new[i][j] = arr[arr.size-1-j][i]

for all 0 <= i <= arr.size-1 and 0 <= j <= arr.size-1. We can therefore compute arr_new as follows:

arr_size = arr.size
  #=> 3 
last_idx = arr_size - 1
  #=> 2 
Array.new(arr_size) { |i| Array.new(arr_size) { |j| arr[j][last_idx-i] } }
  #=> [[8, 2, 6],
  #    [5, 7, 9],
  #    [1, 4, 3]]
1
On

The answer to your error is on this line:

mat1_size = matrix_input.size -1

-1 is a negative number, and Ruby is interpreting it as:

mat1_size = matrix_input.size(-1)

The fix is of course:

mat1_size = matrix_input.size - 1

However your program still has some bugs, but this is the answer to your original question.