How do I rotate this 2-D array using Ruby? -


i'm working through practice problems sharpen ruby skills , i'm working on problem have rotate 2-d array 90 degrees. input:

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

and expected output is:

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

this code have written printing out incorrect output:

def matrix(image)     y = image.length     in 0..y-1         j in 0..y-1             image[i][j] = image[i][y-j-1]         end     end     image end 

this output code:

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

can please explain me doing wrong?

image = [         [1,2,3,4],         [5,6,7,8],         [9,0,1,2],         [3,4,5,6]     ]  y = image.length new_arr = [] in 0..y-1   new_arr << image[i].dup   j in 0..y-1     if y-j-1 == || > y-j-1       image[i][j] = new_arr[y-j-1][i]     else       image[i][j] = image[y-j-1][i]     end   end end  #=> [[3, 9, 5, 1], [4, 0, 6, 2], [5, 1, 7, 3], [6, 2, 8, 4]] 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -