Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def swapDiagonals(matrix)
- array = Array.new(matrix.length){Array.new matrix[0].length}
- first_diagonal = Array.new
- second_diagonal = Array.new
- x = 0
- y = 0
- # get first longest diagonal
- while (x < matrix.length)
- first_diagonal[x] = matrix[x][y]
- x = x + 1
- y = y + 1
- end
- # get second longest diagonal
- x = 0
- y = matrix[0].length - 1
- while (x < matrix.length)
- second_diagonal[x] = matrix[x][y]
- x = x + 1
- y = y - 1
- end
- # swap the two diagonals
- x = 0
- y = 0
- while (x < matrix.length)
- array[x][y] = second_diagonal[x]
- x = x + 1
- y = y + 1
- end
- x = 0
- y = matrix[0].length - 1
- while (x < matrix.length)
- array[x][y] = first_diagonal[x]
- x = x + 1
- y = y - 1
- end
- x = 0
- y = 0
- # fill in the rest
- while (x < matrix.length)
- while (y < matrix[0].length)
- if array[x][y] == nil then
- array[x][y] = matrix[x][y]
- end
- y = y + 1
- end
- x = x + 1
- y = 0
- end
- return array
- end
Advertisement
Add Comment
Please, Sign In to add comment