array-symmetries 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/array.rb +7 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9842cec8943393c58825134c143058f6f4f8d464179f06db6f628b11a20a4c8
|
4
|
+
data.tar.gz: 5e8dae59ed50454965fc892b1d934684d04cdcb35a2ce743cb2727c6a3aa1ca0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40a35fca610a50f47c84e8ca41d15a8923eaaf56282fe9008d0fa057773271b5aa7266db5ebabdfd0fba9c1440ed96e69edc62b073ae3de3560701763a652683
|
7
|
+
data.tar.gz: 92ebf86744bae7ace0831f795526fec94fae4823d36596b3c309ebc20fd3e01566edcad8f52259cbba8f76c464dd6510c87e7c915c74e5ed950336c1c10046b2
|
data/lib/array.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
|
+
# see https://en.wikipedia.org/wiki/Examples_of_groups#The_symmetry_group_of_a_square:_dihedral_group_of_order_8
|
2
|
+
|
1
3
|
class Array # yeah it's a monkey patch
|
4
|
+
# Returns `true` if the array is square, which is to say, the length of each
|
5
|
+
# row is equal to the number of rows. Otherwise returns `false`.
|
2
6
|
def square?
|
3
7
|
all? {|e| e.length == length}
|
4
8
|
end
|
5
9
|
|
6
|
-
# see https://en.wikipedia.org/wiki/Examples_of_groups#The_symmetry_group_of_a_square:_dihedral_group_of_order_8
|
7
10
|
|
11
|
+
# Returns the array obtained by flipping the square array vertically.
|
8
12
|
def b
|
9
13
|
raise ArgumentError unless square?
|
10
14
|
reverse
|
11
15
|
end
|
12
16
|
|
17
|
+
# Returns the array obtained by rotating the square 90° clockwise.
|
13
18
|
def a
|
14
19
|
raise ArgumentError unless square?
|
15
20
|
b.transpose
|
16
21
|
end
|
17
22
|
|
23
|
+
# Returns an array containing the eight symmetries of the square array.
|
18
24
|
def symmetries
|
19
25
|
raise ArgumentError unless square?
|
20
26
|
[self, b, a, a.a, a.a.a, a.b, a.a.b, a.a.a.b]
|