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>'
If I correctly understand what you are trying to accomplish, it can be done as follows.
Note that
arr.map(&:reverse)
is roughly shorthand forNote also that
returns the original array.
I see from the comment that the OP forgot to mention that neither
Array#transpose
orArray#reverse
can be used. In that case we can take a different approach.Observe that if
arr_new
is the array being constructed, then:for all
0 <= i <= arr.size-1
and0 <= j <= arr.size-1
. We can therefore computearr_new
as follows: