-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.rb
More file actions
65 lines (50 loc) · 1.25 KB
/
Copy pathmatrices.rb
File metadata and controls
65 lines (50 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Matrices
def initialize(matrix)
@matrix = matrix
end
def rotate_clockwise
duplicate_matrix = @matrix.map { |row| row.dup }
n = duplicate_matrix.size
(0...n).each do |x|
(n - 1).downto(0) do |y|
duplicate_matrix[x][n - y - 1] = @matrix[y][x]
end
end
duplicate_matrix
end
def rotate_asymmetrical
rows = @matrix.size
columns = @matrix.first.size
rotated_matrix = (0...columns).map { |i| [0] * rows }
(0...columns).each do |column|
(0...rows).each do |row|
rotated_matrix[column][row] = @matrix[rows - 1 - row][column]
end
end
rotated_matrix
end
def rotate_anticlockwise
duplicate_matrix = @matrix.map { |row| row.dup }
n = duplicate_matrix.size
(0...n).each do |x|
(n - 1).downto(0) do |y|
duplicate_matrix[x][n - y - 1] = @matrix[n - 1 - y][n - 1 - x]
end
end
duplicate_matrix
end
def inspect
self.class.display(@matrix)
end
def display(matrix)
self.class.display(matrix)
end
def self.display(matrix)
matrix.each do |row|
puts row.join('|')
end
end
end
matrix = [[1,2,3], [4,5,6], [7, 8, 9], [10, 11, 12]]
matrix = Matrices.new matrix
Matrices.display(matrix.rotate_asymmetrical)