combinatorics 0.3.0 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +1 -1
- data/.gemtest +0 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +4 -5
- data/Benchmarks.md +281 -0
- data/ChangeLog.md +42 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +1 -2
- data/README.md +178 -69
- data/Rakefile +7 -27
- data/benchmarks/cartesian_product.rb +18 -0
- data/benchmarks/choose.rb +19 -0
- data/benchmarks/derange.rb +18 -0
- data/benchmarks/list_comprehension.rb +20 -4
- data/benchmarks/permute.rb +18 -0
- data/benchmarks/power_set.rb +18 -0
- data/combinatorics.gemspec +54 -83
- data/gemspec.yml +25 -0
- data/lib/combinatorics/cartesian_product/cardinality.rb +45 -0
- data/lib/combinatorics/cartesian_product/extensions/array.rb +7 -0
- data/lib/combinatorics/cartesian_product/extensions/set.rb +9 -0
- data/lib/combinatorics/cartesian_product/extensions.rb +2 -0
- data/lib/combinatorics/cartesian_product/mixin.rb +57 -0
- data/lib/combinatorics/cartesian_product.rb +3 -0
- data/lib/combinatorics/choose/cardinality.rb +99 -0
- data/lib/combinatorics/choose/extensions/array.rb +5 -0
- data/lib/combinatorics/choose/extensions/set.rb +6 -0
- data/lib/combinatorics/choose/extensions.rb +2 -0
- data/lib/combinatorics/choose/mixin.rb +53 -0
- data/lib/combinatorics/choose.rb +3 -0
- data/lib/combinatorics/derange/cardinality.rb +23 -0
- data/lib/combinatorics/derange/extensions/array.rb +5 -0
- data/lib/combinatorics/derange/extensions.rb +1 -0
- data/lib/combinatorics/derange/mixin.rb +47 -0
- data/lib/combinatorics/derange.rb +3 -0
- data/lib/combinatorics/enumerator.rb +2 -0
- data/lib/combinatorics/extensions/math.rb +176 -0
- data/lib/combinatorics/extensions/range.rb +2 -2
- data/lib/combinatorics/generator.rb +7 -4
- data/lib/combinatorics/list_comprehension.rb +6 -1
- data/lib/combinatorics/permute/cardinality.rb +98 -0
- data/lib/combinatorics/permute/extensions/array.rb +7 -0
- data/lib/combinatorics/permute/extensions/set.rb +9 -0
- data/lib/combinatorics/permute/extensions.rb +2 -0
- data/lib/combinatorics/permute/mixin.rb +48 -0
- data/lib/combinatorics/permute.rb +3 -0
- data/lib/combinatorics/power_set/cardinality.rb +36 -0
- data/lib/combinatorics/power_set/mixin.rb +19 -22
- data/lib/combinatorics/power_set.rb +1 -0
- data/lib/combinatorics/version.rb +4 -0
- data/lib/combinatorics.rb +8 -0
- data/spec/cartesian_product/array_spec.rb +10 -0
- data/spec/cartesian_product/cardinality_spec.rb +64 -0
- data/spec/cartesian_product/mixin_examples.rb +98 -0
- data/spec/cartesian_product/set_spec.rb +10 -0
- data/spec/choose/array_spec.rb +9 -0
- data/spec/choose/cardinality_spec.rb +132 -0
- data/spec/choose/mixin_examples.rb +48 -0
- data/spec/choose/set_spec.rb +9 -0
- data/spec/combinatorics_spec.rb +5 -1
- data/spec/derange/array_spec.rb +10 -0
- data/spec/derange/cardinality_spec.rb +14 -0
- data/spec/derange/mixin_examples.rb +52 -0
- data/spec/enumerator_spec.rb +1 -1
- data/spec/extensions/math_spec.rb +100 -0
- data/spec/extensions/range_spec.rb +13 -13
- data/spec/generator_spec.rb +1 -1
- data/spec/list_comprehension_spec.rb +11 -11
- data/spec/permute/array_spec.rb +10 -0
- data/spec/permute/cardinality_spec.rb +146 -0
- data/spec/permute/mixin_examples.rb +42 -0
- data/spec/permute/set_spec.rb +10 -0
- data/spec/power_set/array_spec.rb +3 -2
- data/spec/power_set/cardinality_spec.rb +32 -0
- data/spec/power_set/mixin_examples.rb +17 -8
- data/spec/power_set/set_spec.rb +3 -2
- data/spec/spec_helper.rb +3 -3
- metadata +106 -104
- data/VERSION +0 -1
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'combinatorics/cartesian_product/mixin'
|
4
|
+
|
5
|
+
shared_examples_for "CartesianProduct::Mixin" do
|
6
|
+
it "the cartprod of any two Set's should return an Enumerator" do
|
7
|
+
set = subject[1]
|
8
|
+
results = set.cartprod(set)
|
9
|
+
|
10
|
+
expect(results).to be_kind_of(Enumerator)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "the cartprod of two empty Set's should return an empty Set" do
|
14
|
+
set = subject[]
|
15
|
+
results = set.cartprod([]).to_a
|
16
|
+
|
17
|
+
expect(results).to be_empty
|
18
|
+
end
|
19
|
+
|
20
|
+
it "the cartprod of a single empty set should return an empty Set" do
|
21
|
+
set = subject[1,2]
|
22
|
+
results = set.cartprod([2,3],[]).to_a
|
23
|
+
|
24
|
+
expect(results).to be_empty
|
25
|
+
end
|
26
|
+
|
27
|
+
it "the cartprod of another empty set should also return an empty Set" do
|
28
|
+
set = subject[]
|
29
|
+
results = set.cartprod([1]).to_a
|
30
|
+
|
31
|
+
expect(results).to be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it "the cartprod of [1] and [1] should be [[1, 1]]" do
|
35
|
+
set = subject[1]
|
36
|
+
results = set.cartprod([1]).to_a
|
37
|
+
|
38
|
+
expect(results).to eq([[1, 1]])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "the cartprod of [1, 2] and [3] should be [[1, 3], [2, 3]]" do
|
42
|
+
set = subject[1, 2]
|
43
|
+
results = set.cartprod([3]).to_a
|
44
|
+
|
45
|
+
expect(results).to match_array([[1, 3], [2, 3]])
|
46
|
+
end
|
47
|
+
|
48
|
+
it "the cartprod of [1, 2] and [3, 4] should be [[1, 3], [1, 4], [2, 3], [2, 4]]" do
|
49
|
+
set = subject[1, 2]
|
50
|
+
results = set.cartprod([3, 4]).to_a
|
51
|
+
|
52
|
+
expect(results).to match_array([
|
53
|
+
[1, 3], [1, 4],
|
54
|
+
[2, 3], [2, 4]
|
55
|
+
])
|
56
|
+
end
|
57
|
+
|
58
|
+
it "the cartprod of ['a'].cartprod(['b', 'c', 'd']) should be [['a', 'b'], ['a', 'c'], ['a', 'd']]" do
|
59
|
+
set1 = subject['a']
|
60
|
+
set2 = subject['b', 'c', 'd']
|
61
|
+
results = set1.cartprod(set2).to_a
|
62
|
+
|
63
|
+
expect(results).to match_array([['a', 'b'], ['a', 'c'], ['a', 'd']])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "the cartprod of [0, 1] and [[2, 3], [4, 5]] should be [[0, 2, 4], [1, 2, 4], [0, 3, 4], [1, 3, 4], [0, 2, 5], [1, 2, 5], [0, 3, 5], [1, 3, 5]]" do
|
67
|
+
set1 = subject[0, 1]
|
68
|
+
set2 = subject[2, 3]
|
69
|
+
set3 = subject[4, 5]
|
70
|
+
results = set1.cartprod(set2, set3).to_a
|
71
|
+
|
72
|
+
expect(results).to match_array([
|
73
|
+
[0, 2, 4], [0, 2, 5], [0, 3, 4], [0, 3, 5],
|
74
|
+
[1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5],
|
75
|
+
])
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should take an optional block argument" do
|
79
|
+
set = subject[1]
|
80
|
+
results = []
|
81
|
+
|
82
|
+
set.cartprod(set) { |result| results << result }
|
83
|
+
|
84
|
+
expect(results).to eq([[1, 1]])
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should alias cartprod to cartesian_product" do
|
88
|
+
aset = subject[1]
|
89
|
+
|
90
|
+
expect(aset).to respond_to(:cartesian_product)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should alias cartprod to cartesian" do
|
94
|
+
aset = subject[1]
|
95
|
+
|
96
|
+
expect(aset).to respond_to(:cartesian)
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'combinatorics/choose'
|
3
|
+
|
4
|
+
describe Choose do
|
5
|
+
subject { Choose }
|
6
|
+
|
7
|
+
describe "cardinality" do
|
8
|
+
it "should raise RangeError if n is negative" do
|
9
|
+
expect { subject.cardinality(-1) }.to raise_error(RangeError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should raise RangeError if n is negative" do
|
13
|
+
expect { subject.cardinality(-1, 1) }.to raise_error(RangeError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should raise RangeError if r is negative" do
|
17
|
+
expect { subject.cardinality(1, -1) }.to raise_error(RangeError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should raise RangeError if r is greater than n" do
|
21
|
+
expect { subject.cardinality(2, 3) }.to raise_error(RangeError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return 1 for subject.cardinality(0)" do
|
25
|
+
expect(subject.cardinality(0)).to eq(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return 1 for subject.cardinality(1)" do
|
29
|
+
expect(subject.cardinality(1)).to eq(1)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return 2 for subject.cardinality(2)" do
|
33
|
+
expect(subject.cardinality(2)).to eq(2)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return 6 for subject.cardinality(3)" do
|
37
|
+
expect(subject.cardinality(3)).to eq(6)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return 24 for subject.cardinality(4)" do
|
41
|
+
expect(subject.cardinality(4)).to eq(24)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return 1 for subject.cardinality(1, 0)" do
|
45
|
+
expect(subject.cardinality(1, 0)).to eq(1)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return 1 for subject.cardinality(1, 1)" do
|
49
|
+
expect(subject.cardinality(1, 1)).to eq(1)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return 2 for subject.cardinality(2, 1)" do
|
53
|
+
expect(subject.cardinality(2, 1)).to eq(2)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return 1 for subject.cardinality(2, 2)" do
|
57
|
+
expect(subject.cardinality(2, 2)).to eq(1)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return 3 for subject.cardinality(3, 1)" do
|
61
|
+
expect(subject.cardinality(3, 1)).to eq(3)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return 3 for subject.cardinality(3, 2)" do
|
65
|
+
expect(subject.cardinality(3, 2)).to eq(3)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return 1 for subject.cardinality(3, 3)" do
|
69
|
+
expect(subject.cardinality(3, 3)).to eq(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return 4 for subject.cardinality(4, 1)" do
|
73
|
+
expect(subject.cardinality(4, 1)).to eq(4)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return 6 for subject.cardinality(4, 2)" do
|
77
|
+
expect(subject.cardinality(4, 2)).to eq(6)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return 4 for subject.cardinality(4, 3)" do
|
81
|
+
expect(subject.cardinality(4, 3)).to eq(4)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return 1 for subject.cardinality(4, 4)" do
|
85
|
+
expect(subject.cardinality(4, 4)).to eq(1)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return 15 for subject.cardinality(6, 4)" do
|
89
|
+
expect(subject.cardinality(6, 4)).to eq(15)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return 3628800 for subject.cardinality(10)" do
|
93
|
+
expect(subject.cardinality(10)).to eq(3628800)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "cardinality_all" do
|
98
|
+
it "should return [] for subject.cardinality_all(0)" do
|
99
|
+
expect(subject.cardinality_all(0)).to be_empty
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return [1] for subject.cardinality_all(1)" do
|
103
|
+
expect(subject.cardinality_all(1)).to eq([1])
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return [2, 1] for subject.cardinality_all(2)" do
|
107
|
+
expect(subject.cardinality_all(2)).to eq([2, 1])
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should return [3, 3, 1] for subject.cardinality_all(3)" do
|
111
|
+
expect(subject.cardinality_all(3)).to eq([3, 3, 1])
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should return [4, 6, 4, 1] for subject.cardinality_all(4)" do
|
115
|
+
expect(subject.cardinality_all(4)).to eq([4, 6, 4, 1])
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should allow specifying the range of `r` values" do
|
119
|
+
expect(subject.cardinality_all(10,5..10)).to eq([
|
120
|
+
252, 210, 120, 45, 10, 1
|
121
|
+
])
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should raise RangeError for subject.cardinality_all(-1)" do
|
125
|
+
expect { subject.cardinality_all(-1) }.to raise_error(RangeError)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should wrap cardinality with Choose.C" do
|
129
|
+
should respond_to(:C)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'combinatorics/choose'
|
3
|
+
|
4
|
+
shared_examples_for "Choose::Mixin" do
|
5
|
+
let(:empty_set) { Set[] }
|
6
|
+
|
7
|
+
it "should return [[]] for [].choose(0).to_a" do
|
8
|
+
set = subject[]
|
9
|
+
results = set.choose(0).to_a
|
10
|
+
|
11
|
+
expect(results).to eq([empty_set])
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return [[]] for [1].choose(0).to_a" do
|
15
|
+
set = subject[1]
|
16
|
+
results = set.choose(0).to_a
|
17
|
+
|
18
|
+
expect(results).to eq([empty_set])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return [[1]] for [1].choose(1).to_a" do
|
22
|
+
set = subject[1]
|
23
|
+
results = set.choose(1).to_a
|
24
|
+
|
25
|
+
expect(results).to eq([Set[1]])
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return [[1], [2]] for [1, 2].choose(1).to_a" do
|
29
|
+
set = subject[1, 2]
|
30
|
+
results = set.choose(1).to_a
|
31
|
+
|
32
|
+
expect(results).to match_array([Set[1], Set[2]])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return [[1, 2]] for [1, 2].choose(2).to_a" do
|
36
|
+
set = subject[1, 2]
|
37
|
+
results = set.choose(2).to_a
|
38
|
+
|
39
|
+
expect(results).to eq([Set[1, 2]])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should filter out repeated elements" do
|
43
|
+
set1 = subject[1,1,2,3]
|
44
|
+
set2 = subject[1,2,3]
|
45
|
+
|
46
|
+
expect(set1.choose(2).to_a).to eq(set2.choose(2).to_a)
|
47
|
+
end
|
48
|
+
end
|
data/spec/combinatorics_spec.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'combinatorics/derange/cardinality'
|
3
|
+
|
4
|
+
describe Derange do
|
5
|
+
subject { Derange }
|
6
|
+
|
7
|
+
it "should alias cardinality to subfactorial" do
|
8
|
+
should respond_to(:cardinality)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should wrap subfactorial with Derange.D" do
|
12
|
+
should respond_to(:D)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'combinatorics/derange'
|
4
|
+
|
5
|
+
shared_examples_for "Derange::Mixin" do
|
6
|
+
it "the derange of an empty Set should only contain an empty Array" do
|
7
|
+
set = subject[]
|
8
|
+
results = set.derange.to_a
|
9
|
+
|
10
|
+
expect(results).to eq([[]])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return [[]] for [1].derange.to_a" do
|
14
|
+
set = subject[1]
|
15
|
+
results = set.derange.to_a
|
16
|
+
|
17
|
+
expect(results).to eq([[]])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return [[2, 1]] for [1, 2].derange.to_a" do
|
21
|
+
set = subject[1, 2]
|
22
|
+
results = set.derange.to_a
|
23
|
+
|
24
|
+
expect(results).to eq([[2, 1]])
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return [[2, 1, 4, 3], [2, 3, 4, 1], [2, 4, 1, 3], [3, 1, 4, 2], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 3, 1, 2], [4, 3, 2, 1]] for [1, 2, 3, 4].derange.to_a" do
|
28
|
+
set = [1, 2, 3, 4]
|
29
|
+
results = set.derange.to_a
|
30
|
+
|
31
|
+
expect(results).to eq([
|
32
|
+
[2, 1, 4, 3],
|
33
|
+
[2, 3, 4, 1],
|
34
|
+
[2, 4, 1, 3],
|
35
|
+
[3, 1, 4, 2],
|
36
|
+
[3, 4, 1, 2],
|
37
|
+
[3, 4, 2, 1],
|
38
|
+
[4, 1, 2, 3],
|
39
|
+
[4, 3, 1, 2],
|
40
|
+
[4, 3, 2, 1]
|
41
|
+
])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should take an optional block argument" do
|
45
|
+
set = subject[1, 2, 3]
|
46
|
+
results = []
|
47
|
+
|
48
|
+
set.derange { |deranged| results << deranged }
|
49
|
+
|
50
|
+
expect(results).to eq([[2, 3, 1], [3, 1, 2]])
|
51
|
+
end
|
52
|
+
end
|
data/spec/enumerator_spec.rb
CHANGED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'combinatorics/extensions/math'
|
3
|
+
|
4
|
+
describe Math do
|
5
|
+
it "should alias Sigma to sigma" do
|
6
|
+
should respond_to(:Sigma)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should alias Pi to pi" do
|
10
|
+
should respond_to(:Pi)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "sigma" do
|
14
|
+
it "should return 6 for sigma(1..3)" do
|
15
|
+
expect(subject.sigma(1..3)).to eq(6)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return 60 for sigma(3..5)" do
|
19
|
+
expect(subject.sigma(3..5)).to eq(12)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should take an optional block argument" do
|
23
|
+
result = subject.sigma(1..5) { |i| i * 2 }
|
24
|
+
|
25
|
+
expect(result).to eq((1 * 2) + (2 * 2) + (3 * 2) + (4 * 2) + (5 * 2))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "pi" do
|
30
|
+
it "should return 24 for pi(1..4)" do
|
31
|
+
expect(subject.pi(1..4)).to eq(24)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return 30 for pi(5..6)" do
|
35
|
+
expect(subject.pi(5..6)).to eq(30)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should take an optional block argument" do
|
39
|
+
result = subject.pi(1..3) { |i| i * 2 }
|
40
|
+
|
41
|
+
expect(result).to eq((1 * 2) * (2 * 2) * (3 * 2))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "factorial" do
|
46
|
+
it "should return 1 for factorial(0)" do
|
47
|
+
expect(subject.factorial(0)).to eq(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return 1 for factorial(1)" do
|
51
|
+
expect(subject.factorial(1)).to eq(1)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return 2 for factorial(2)" do
|
55
|
+
expect(subject.factorial(2)).to eq(2)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return 6 for factorial(3)" do
|
59
|
+
expect(subject.factorial(3)).to eq(6)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return 3628800 for factorial(10)" do
|
63
|
+
expect(subject.factorial(10)).to eq(3628800)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should raise RangeError for factorial(-1)" do
|
67
|
+
expect { subject.factorial(-1) }.to raise_error(RangeError)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "subfactorial" do
|
72
|
+
it "should return 1 for subfactorial(0)" do
|
73
|
+
expect(subject.subfactorial(0)).to eq(1)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return 0 for subfactorial(1)" do
|
77
|
+
expect(subject.subfactorial(1)).to eq(0)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return 1 for subfactorial(2)" do
|
81
|
+
expect(subject.subfactorial(2)).to eq(1)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return 2 for subfactorial(3)" do
|
85
|
+
expect(subject.subfactorial(3)).to eq(2)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return 9 for subfactorial(4)" do
|
89
|
+
expect(subject.subfactorial(4)).to eq(9)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return 44 for subfactorial(5)" do
|
93
|
+
expect(subject.subfactorial(5)).to eq(44)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should raise RangeError for subfactorial(-1)" do
|
97
|
+
expect { subject.subfactorial(-1) }.to raise_error(RangeError)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -4,11 +4,11 @@ require 'combinatorics/extensions/range'
|
|
4
4
|
describe Range do
|
5
5
|
describe "&" do
|
6
6
|
it "should pick the maximum beginning value" do
|
7
|
-
((100..200) & (150..200)).first.
|
7
|
+
expect(((100..200) & (150..200)).first).to eq(150)
|
8
8
|
end
|
9
9
|
|
10
|
-
it "should pick the
|
11
|
-
((100..150) & (100..200)).last.
|
10
|
+
it "should pick the minimum ending value" do
|
11
|
+
expect(((100..150) & (100..200)).last).to eq(150)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -16,28 +16,28 @@ describe Range do
|
|
16
16
|
subject { 1..10 }
|
17
17
|
|
18
18
|
it "should iterate over every beginning value" do
|
19
|
-
subject.upto(5..10).to_a.
|
19
|
+
expect(subject.upto(5..10).to_a).to eq([
|
20
20
|
(1..10),
|
21
21
|
(2..10),
|
22
22
|
(3..10),
|
23
23
|
(4..10),
|
24
24
|
(5..10)
|
25
|
-
]
|
25
|
+
])
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should iterate over every ending value" do
|
29
|
-
subject.upto(1..15).to_a.
|
29
|
+
expect(subject.upto(1..15).to_a).to eq([
|
30
30
|
(1..10),
|
31
31
|
(1..11),
|
32
32
|
(1..12),
|
33
33
|
(1..13),
|
34
34
|
(1..14),
|
35
35
|
(1..15)
|
36
|
-
]
|
36
|
+
])
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should not iterate up to lower bounding ranges" do
|
40
|
-
subject.upto(0..5).to_a.
|
40
|
+
expect(subject.upto(0..5).to_a).to be_empty
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -45,28 +45,28 @@ describe Range do
|
|
45
45
|
subject { 5..15 }
|
46
46
|
|
47
47
|
it "should iterate over every beginning value" do
|
48
|
-
subject.downto(1..15).to_a.
|
48
|
+
expect(subject.downto(1..15).to_a).to eq([
|
49
49
|
(5..15),
|
50
50
|
(4..15),
|
51
51
|
(3..15),
|
52
52
|
(2..15),
|
53
53
|
(1..15)
|
54
|
-
]
|
54
|
+
])
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should iterate over every ending value" do
|
58
|
-
subject.downto(5..10).to_a.
|
58
|
+
expect(subject.downto(5..10).to_a).to eq([
|
59
59
|
(5..15),
|
60
60
|
(5..14),
|
61
61
|
(5..13),
|
62
62
|
(5..12),
|
63
63
|
(5..11),
|
64
64
|
(5..10)
|
65
|
-
]
|
65
|
+
])
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should not iterate down to upward bounding ranges" do
|
69
|
-
subject.downto(10..20).to_a.
|
69
|
+
expect(subject.downto(10..20).to_a).to be_empty
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
data/spec/generator_spec.rb
CHANGED
@@ -6,46 +6,46 @@ describe "Array#comprehension" do
|
|
6
6
|
it "should return an Enumerator object if no block is given" do
|
7
7
|
a = [1..5]
|
8
8
|
|
9
|
-
a.comprehension.
|
9
|
+
expect(a.comprehension).not_to be_kind_of(Array)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should yield iterations to the given block" do
|
13
13
|
range = (1..5)
|
14
14
|
a = [range]
|
15
15
|
|
16
|
-
a.comprehension.to_a.
|
16
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should do nothing an Array of all non-enumerable objects" do
|
20
20
|
a = [1,2,3]
|
21
21
|
|
22
|
-
a.comprehension.to_a.
|
22
|
+
expect(a.comprehension.to_a).to eq([a])
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should pass through an empty Array" do
|
26
26
|
a = []
|
27
27
|
|
28
|
-
a.comprehension.to_a.
|
28
|
+
expect(a.comprehension.to_a).to eq([a])
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should iterate over the values within an enumerable value" do
|
32
32
|
range = (1..5)
|
33
33
|
a = [range]
|
34
34
|
|
35
|
-
a.comprehension.to_a.
|
35
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should iterate over repeating values" do
|
39
39
|
range = [1,2,3,1,2,4]
|
40
40
|
a = [range]
|
41
41
|
|
42
|
-
a.comprehension.to_a.
|
42
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[1],[2],[4]])
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should iterate over values from a generator" do
|
46
46
|
a = [Combinatorics::Generator.new { |g| 5.times { |i| g.yield i } }]
|
47
47
|
|
48
|
-
a.comprehension.to_a.
|
48
|
+
expect(a.comprehension.to_a).to eq([[0],[1],[2],[3],[4]])
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should iterate over values from a non-repeating generator" do
|
@@ -54,20 +54,20 @@ describe "Array#comprehension" do
|
|
54
54
|
[1,2],
|
55
55
|
Combinatorics::Generator.new { |g|
|
56
56
|
multiplier += 1
|
57
|
-
5.times { |i| g.yield
|
57
|
+
5.times { |i| g.yield(i * multiplier) }
|
58
58
|
}
|
59
59
|
]
|
60
60
|
|
61
|
-
a.comprehension.to_a.
|
61
|
+
expect(a.comprehension.to_a).to eq([
|
62
62
|
[1,0],[1,1],[1,2],[1,3],[1,4],
|
63
63
|
[2,0],[2,2],[2,4],[2,6],[2,8]
|
64
|
-
]
|
64
|
+
])
|
65
65
|
end
|
66
66
|
|
67
67
|
it "should ignore non-enumerable values" do
|
68
68
|
range = (1..5)
|
69
69
|
a = [1,range]
|
70
70
|
|
71
|
-
a.comprehension.to_a.
|
71
|
+
expect(a.comprehension.to_a).to eq([[1,1],[1,2],[1,3],[1,4],[1,5]])
|
72
72
|
end
|
73
73
|
end
|