Last active
August 24, 2019 18:34
-
-
Save vbrazo/bc1d4ad0fcc1c3298d867b034cf5af2c to your computer and use it in GitHub Desktop.
Revisions
-
vbrazo revised this gist
Aug 24, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # # Transpose a matrix # Make [[1,2,3], [4,5,6], [7,8,9]] becomes [[1,4,7], [2,5,8], [3,6,9]] # class Array def my_transpose -
vbrazo revised this gist
Aug 24, 2019 . No changes.There are no files selected for viewing
-
vbrazo revised this gist
Aug 24, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ def my_reverse(array) array.each do reverse << array[i] i -= 1 end reverse -
vbrazo revised this gist
Aug 24, 2019 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,5 +46,4 @@ def rotate90(array) 2.5.1 :1736 > rotate90([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) => [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 2.5.1 :1737 > rotate90([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) => [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]] -
vbrazo renamed this gist
Aug 24, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
vbrazo created this gist
Aug 24, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ # # Transpose a matrix # Make [[1,2,3],[4,5,6],[7,8,9]] becomes [[1,4,7], [2,5,8], [3,6,9]] # class Array def my_transpose size.times do |i| 0.upto(i) do |j| self[i][j], self[j][i] = self[j][i], self[i][j] end end self end end # # Reverse array # Make [1,4,7] becomes [7,4,1] # def my_reverse(array) reverse = [] i = -1 array.each do reverse << array[i] i-=1 end reverse end # # Rotate matrix 90 # def rotate90(array) array = array.my_transpose array.map { |row| my_reverse(row) } end rotate90([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) rotate90([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) # # IRB console: # 2.5.1 :1736 > rotate90([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) => [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 2.5.1 :1737 > rotate90([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) => [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]