combinatorics 0.3.0 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e0761711a88421b7b0214a3b585b6a843401bd1e84bb189c87a0c4167d7e5f32
|
4
|
+
data.tar.gz: 6c42e53311328f5d0f6c27ea339c3539046fee352dfb548155d35183a19d5aef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d9df890fc1669969b84e48a0b3d6126111cc6e13369308f76c274e0227fcd475a9047872ef247dc83b009f4b5d234526aa9d88c0c59cff7c9129d080c93a044
|
7
|
+
data.tar.gz: 53eabbc7025dbaa9e79d8fe44c8d68c0182de3b575f425f6b4d0231c88920a8ee7b8c85bfd8f94743b8a618184525d0db5264d37232bd8793396b93afbfb1a0f
|
data/.document
CHANGED
data/.gemtest
ADDED
File without changes
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [ push, pull_request ]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
tests:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
ruby:
|
12
|
+
- 2.6
|
13
|
+
- 2.7
|
14
|
+
- '3.0'
|
15
|
+
- 3.1
|
16
|
+
- jruby
|
17
|
+
- truffleruby
|
18
|
+
name: Ruby ${{ matrix.ruby }}
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v2
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
25
|
+
- name: Install dependencies
|
26
|
+
run: bundle install --jobs 4 --retry 3
|
27
|
+
- name: Run tests
|
28
|
+
run: bundle exec rake test
|
data/.gitignore
CHANGED
data/Benchmarks.md
ADDED
@@ -0,0 +1,281 @@
|
|
1
|
+
# Benchmarks
|
2
|
+
|
3
|
+
Benchmarks for the {Combinatorics} code can be found in the `benchmarks/`
|
4
|
+
directory.
|
5
|
+
|
6
|
+
Benchmark machine specs:
|
7
|
+
|
8
|
+
* CPU: AMD Athlon(tm) 64 Processor 3400+ (2202.785 MHz)
|
9
|
+
* Cache Size: 1024 KB
|
10
|
+
* Memory: 1 Gb
|
11
|
+
|
12
|
+
## List Comprehensions:
|
13
|
+
|
14
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
15
|
+
|
16
|
+
user system total real
|
17
|
+
singleton: 0.050000 0.020000 0.070000 ( 0.081225)
|
18
|
+
single-enum: 0.000000 0.000000 0.000000 ( 0.009706)
|
19
|
+
depth 1: 0.000000 0.000000 0.000000 ( 0.000652)
|
20
|
+
depth 2: 0.070000 0.010000 0.080000 ( 0.084974)
|
21
|
+
depth 3: 5.670000 0.240000 5.910000 ( 6.623347)
|
22
|
+
|
23
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
24
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
25
|
+
|
26
|
+
user system total real
|
27
|
+
singleton: 1.365000 0.000000 1.365000 ( 1.212000)
|
28
|
+
single-enum: 0.134000 0.000000 0.134000 ( 0.134000)
|
29
|
+
depth 1: 0.046000 0.000000 0.046000 ( 0.045000)
|
30
|
+
depth 2: 1.613000 0.000000 1.613000 ( 1.614000)
|
31
|
+
depth 3: 10.054000 0.000000 10.054000 ( 10.054000)
|
32
|
+
|
33
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
34
|
+
|
35
|
+
user system total real
|
36
|
+
singleton: 0.802878 0.112983 0.915861 ( 1.722362)
|
37
|
+
single-enum: 0.128980 0.011998 0.140978 ( 0.176392)
|
38
|
+
depth 1: 0.000000 0.000000 0.000000 ( 0.000897)
|
39
|
+
depth 2: 0.058991 0.004999 0.063990 ( 0.143435)
|
40
|
+
depth 3: 3.483470 0.148977 3.632447 ( 5.156510)
|
41
|
+
|
42
|
+
## Cartesian Product
|
43
|
+
|
44
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
45
|
+
|
46
|
+
user system total real
|
47
|
+
{100} x {100} 0.070000 0.000000 0.070000 ( 0.084734)
|
48
|
+
{200} x {200} 0.290000 0.020000 0.310000 ( 0.347752)
|
49
|
+
{400} x {400} 1.370000 0.070000 1.440000 ( 1.582130)
|
50
|
+
{800} x {800} 8.250000 0.370000 8.620000 ( 9.498290)
|
51
|
+
|
52
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
53
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
54
|
+
|
55
|
+
user system total real
|
56
|
+
{100} x {100} 2.469000 0.000000 2.469000 ( 2.349000)
|
57
|
+
{200} x {200} 2.575000 0.000000 2.575000 ( 2.574000)
|
58
|
+
{400} x {400} 2.354000 0.000000 2.354000 ( 2.354000)
|
59
|
+
{800} x {800} 7.730000 0.000000 7.730000 ( 7.730000)
|
60
|
+
|
61
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
62
|
+
|
63
|
+
user system total real
|
64
|
+
{100} x {100} 0.114982 0.051992 0.166974 ( 1.184129)
|
65
|
+
{200} x {200} 0.391941 0.159976 0.551917 ( 1.396105)
|
66
|
+
{400} x {400} 1.333797 0.653901 1.987698 ( 3.120384)
|
67
|
+
{800} x {800} 5.961093 2.614602 8.575695 ( 13.643119)
|
68
|
+
|
69
|
+
## Choose
|
70
|
+
|
71
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
72
|
+
|
73
|
+
user system total real
|
74
|
+
n=20 k=1 0.000000 0.000000 0.000000 ( 0.000172)
|
75
|
+
n=20 k=2 0.010000 0.000000 0.010000 ( 0.003592)
|
76
|
+
n=20 k=3 0.010000 0.000000 0.010000 ( 0.017710)
|
77
|
+
n=20 k=4 0.040000 0.000000 0.040000 ( 0.060566)
|
78
|
+
n=20 k=5 0.180000 0.010000 0.190000 ( 0.211709)
|
79
|
+
n=20 k=6 0.490000 0.030000 0.520000 ( 0.583004)
|
80
|
+
n=20 k=7 1.110000 0.070000 1.180000 ( 1.307065)
|
81
|
+
n=20 k=8 1.980000 0.050000 2.030000 ( 2.204111)
|
82
|
+
n=20 k=9 2.770000 0.120000 2.890000 ( 3.221468)
|
83
|
+
n=20 k=10 3.310000 0.150000 3.460000 ( 3.904973)
|
84
|
+
n=20 k=11 3.120000 0.150000 3.270000 ( 4.085721)
|
85
|
+
n=20 k=12 2.520000 0.140000 2.660000 ( 3.139445)
|
86
|
+
n=20 k=13 1.570000 0.060000 1.630000 ( 1.853883)
|
87
|
+
n=20 k=14 0.820000 0.040000 0.860000 ( 1.142598)
|
88
|
+
n=20 k=15 0.350000 0.020000 0.370000 ( 0.436363)
|
89
|
+
n=20 k=16 0.110000 0.000000 0.110000 ( 0.141398)
|
90
|
+
n=20 k=17 0.030000 0.010000 0.040000 ( 0.047691)
|
91
|
+
n=20 k=18 0.000000 0.000000 0.000000 ( 0.003746)
|
92
|
+
n=20 k=19 0.000000 0.000000 0.000000 ( 0.000438)
|
93
|
+
n=20 k=20 0.000000 0.000000 0.000000 ( 0.000043)
|
94
|
+
|
95
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
96
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
97
|
+
|
98
|
+
user system total real
|
99
|
+
n=20 k=1 0.271000 0.000000 0.271000 ( 0.048000)
|
100
|
+
n=20 k=2 0.163000 0.000000 0.163000 ( 0.163000)
|
101
|
+
n=20 k=3 0.184000 0.000000 0.184000 ( 0.184000)
|
102
|
+
n=20 k=4 0.759000 0.000000 0.759000 ( 0.759000)
|
103
|
+
n=20 k=5 1.157000 0.000000 1.157000 ( 1.157000)
|
104
|
+
n=20 k=6 1.116000 0.000000 1.116000 ( 1.116000)
|
105
|
+
n=20 k=7 0.490000 0.000000 0.490000 ( 0.490000)
|
106
|
+
n=20 k=8 0.632000 0.000000 0.632000 ( 0.632000)
|
107
|
+
n=20 k=9 0.907000 0.000000 0.907000 ( 0.908000)
|
108
|
+
n=20 k=10 1.040000 0.000000 1.040000 ( 1.040000)
|
109
|
+
n=20 k=11 0.991000 0.000000 0.991000 ( 0.991000)
|
110
|
+
n=20 k=12 0.784000 0.000000 0.784000 ( 0.784000)
|
111
|
+
n=20 k=13 0.513000 0.000000 0.513000 ( 0.514000)
|
112
|
+
n=20 k=14 0.285000 0.000000 0.285000 ( 0.285000)
|
113
|
+
n=20 k=15 0.141000 0.000000 0.141000 ( 0.142000)
|
114
|
+
n=20 k=16 0.045000 0.000000 0.045000 ( 0.045000)
|
115
|
+
n=20 k=17 0.009000 0.000000 0.009000 ( 0.009000)
|
116
|
+
n=20 k=18 0.004000 0.000000 0.004000 ( 0.004000)
|
117
|
+
n=20 k=19 0.001000 0.000000 0.001000 ( 0.001000)
|
118
|
+
n=20 k=20 0.000000 0.000000 0.000000 ( 0.000000)
|
119
|
+
|
120
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
121
|
+
|
122
|
+
user system total real
|
123
|
+
n=20 k=1 0.001000 0.000000 0.001000 ( 0.001005)
|
124
|
+
n=20 k=2 0.005999 0.000000 0.005999 ( 0.016681)
|
125
|
+
n=20 k=3 0.051992 0.002000 0.053992 ( 0.177731)
|
126
|
+
n=20 k=4 0.159976 0.005999 0.165975 ( 0.564428)
|
127
|
+
n=20 k=5 0.412938 0.016998 0.429936 ( 1.410280)
|
128
|
+
n=20 k=6 1.045841 0.034994 1.080835 ( 1.220624)
|
129
|
+
n=20 k=7 2.234661 0.073989 2.308650 ( 2.514775)
|
130
|
+
n=20 k=8 3.821419 0.157976 3.979395 ( 4.479381)
|
131
|
+
n=20 k=9 5.377183 0.230965 5.608148 ( 6.545616)
|
132
|
+
n=20 k=10 6.270047 0.271958 6.542005 ( 7.481663)
|
133
|
+
n=20 k=11 6.056079 0.201970 6.258049 ( 7.179931)
|
134
|
+
n=20 k=12 4.684288 0.237963 4.922251 ( 5.829043)
|
135
|
+
n=20 k=13 3.066534 0.122982 3.189516 ( 3.633006)
|
136
|
+
n=20 k=14 1.716739 0.060990 1.777729 ( 2.083863)
|
137
|
+
n=20 k=15 0.708892 0.031996 0.740888 ( 0.814869)
|
138
|
+
n=20 k=16 0.233965 0.006999 0.240964 ( 0.272663)
|
139
|
+
n=20 k=17 0.057992 0.000999 0.058991 ( 0.075194)
|
140
|
+
n=20 k=18 0.008999 0.001000 0.009999 ( 0.015695)
|
141
|
+
n=20 k=19 0.001999 0.000000 0.001999 ( 0.001539)
|
142
|
+
n=20 k=20 0.000000 0.000000 0.000000 ( 0.000253)
|
143
|
+
|
144
|
+
## Permute
|
145
|
+
|
146
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
147
|
+
|
148
|
+
user system total real
|
149
|
+
r=1 0.000000 0.000000 0.000000 ( 0.000029)
|
150
|
+
r=2 0.000000 0.000000 0.000000 ( 0.000029)
|
151
|
+
r=3 0.010000 0.000000 0.010000 ( 0.002234)
|
152
|
+
r=4 0.000000 0.000000 0.000000 ( 0.002388)
|
153
|
+
r=5 0.020000 0.000000 0.020000 ( 0.028709)
|
154
|
+
r=6 0.100000 0.000000 0.100000 ( 0.119063)
|
155
|
+
r=7 0.380000 0.030000 0.410000 ( 0.462036)
|
156
|
+
r=8 1.210000 0.080000 1.290000 ( 1.447420)
|
157
|
+
r=9 2.510000 0.130000 2.640000 ( 2.919967)
|
158
|
+
r=10 2.840000 0.200000 3.040000 ( 3.664906)
|
159
|
+
|
160
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
161
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
162
|
+
|
163
|
+
user system total real
|
164
|
+
r=1 0.274000 0.000000 0.274000 ( 0.041000)
|
165
|
+
r=2 0.001000 0.000000 0.001000 ( 0.001000)
|
166
|
+
r=3 0.012000 0.000000 0.012000 ( 0.012000)
|
167
|
+
r=4 0.154000 0.000000 0.154000 ( 0.154000)
|
168
|
+
r=5 0.306000 0.000000 0.306000 ( 0.307000)
|
169
|
+
r=6 0.081000 0.000000 0.081000 ( 0.081000)
|
170
|
+
r=7 0.168000 0.000000 0.168000 ( 0.168000)
|
171
|
+
r=8 0.516000 0.000000 0.516000 ( 0.517000)
|
172
|
+
r=9 1.224000 0.000000 1.224000 ( 1.224000)
|
173
|
+
r=10 1.649000 0.000000 1.649000 ( 1.649000)
|
174
|
+
|
175
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
176
|
+
|
177
|
+
user system total real
|
178
|
+
r=1 0.001000 0.000000 0.001000 ( 0.000062)
|
179
|
+
r=2 0.001000 0.000000 0.001000 ( 0.004535)
|
180
|
+
r=3 0.006000 0.000000 0.006000 ( 0.016175)
|
181
|
+
r=4 0.049993 0.003000 0.052993 ( 0.171867)
|
182
|
+
r=5 0.153976 0.012998 0.166974 ( 0.584311)
|
183
|
+
r=6 0.578912 0.020997 0.599909 ( 0.654977)
|
184
|
+
r=7 2.694590 0.106984 2.801574 ( 3.170215)
|
185
|
+
r=8 9.167606 0.337948 9.505554 ( 10.952164)
|
186
|
+
r=9 21.419744 0.785881 22.205625 ( 24.619187)
|
187
|
+
r=10 29.557506 1.087834 30.645340 ( 34.234011)
|
188
|
+
|
189
|
+
## Derange
|
190
|
+
|
191
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
192
|
+
|
193
|
+
user system total real
|
194
|
+
n=1 0.000000 0.000000 0.000000 ( 0.000021)
|
195
|
+
n=2 0.000000 0.000000 0.000000 ( 0.000041)
|
196
|
+
n=3 0.000000 0.000000 0.000000 ( 0.000021)
|
197
|
+
n=4 0.000000 0.000000 0.000000 ( 0.000069)
|
198
|
+
n=5 0.000000 0.000000 0.000000 ( 0.000393)
|
199
|
+
n=6 0.000000 0.000000 0.000000 ( 0.002281)
|
200
|
+
n=7 0.020000 0.010000 0.030000 ( 0.028188)
|
201
|
+
n=8 0.170000 0.000000 0.170000 ( 0.207589)
|
202
|
+
n=9 1.630000 0.070000 1.700000 ( 1.974767)
|
203
|
+
n=10 17.200000 0.670000 17.870000 ( 20.047089)
|
204
|
+
|
205
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
206
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
207
|
+
|
208
|
+
user system total real
|
209
|
+
n=1 0.150000 0.000000 0.150000 ( 0.023000)
|
210
|
+
n=2 0.003000 0.000000 0.003000 ( 0.003000)
|
211
|
+
n=3 0.001000 0.000000 0.001000 ( 0.001000)
|
212
|
+
n=4 0.006000 0.000000 0.006000 ( 0.007000)
|
213
|
+
n=5 0.022000 0.000000 0.022000 ( 0.022000)
|
214
|
+
n=6 0.173000 0.000000 0.173000 ( 0.173000)
|
215
|
+
n=7 1.045000 0.000000 1.045000 ( 1.045000)
|
216
|
+
n=8 0.821000 0.000000 0.821000 ( 0.821000)
|
217
|
+
n=9 1.917000 0.000000 1.917000 ( 1.917000)
|
218
|
+
n=10 25.652000 0.000000 25.652000 ( 25.652000)
|
219
|
+
|
220
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
221
|
+
|
222
|
+
user system total real
|
223
|
+
n=1 0.000000 0.000000 0.000000 ( 0.000025)
|
224
|
+
n=2 0.000000 0.000000 0.000000 ( 0.000394)
|
225
|
+
n=3 0.000000 0.000000 0.000000 ( 0.000145)
|
226
|
+
n=4 0.000000 0.000000 0.000000 ( 0.000637)
|
227
|
+
n=5 0.004000 0.000000 0.004000 ( 0.010315)
|
228
|
+
n=6 0.036994 0.003000 0.039994 ( 0.151349)
|
229
|
+
n=7 0.130980 0.009998 0.140978 ( 0.584286)
|
230
|
+
n=8 0.579912 0.028996 0.608908 ( 0.946738)
|
231
|
+
n=9 5.563154 0.191970 5.755124 ( 6.588485)
|
232
|
+
n=10 59.973882 2.427631 62.401513 ( 80.877847)
|
233
|
+
|
234
|
+
## Power Set
|
235
|
+
|
236
|
+
Ruby 1.9.2-p290 (2011-07-09 revision 32553) [x86_64-linux]:
|
237
|
+
|
238
|
+
user system total real
|
239
|
+
n=10 0.000000 0.000000 0.000000 ( 0.015275)
|
240
|
+
n=11 0.020000 0.000000 0.020000 ( 0.033685)
|
241
|
+
n=12 0.050000 0.000000 0.050000 ( 0.063667)
|
242
|
+
n=13 0.110000 0.010000 0.120000 ( 0.131612)
|
243
|
+
n=14 0.230000 0.010000 0.240000 ( 0.276595)
|
244
|
+
n=15 0.500000 0.010000 0.510000 ( 0.559058)
|
245
|
+
n=16 1.040000 0.050000 1.090000 ( 1.222567)
|
246
|
+
n=17 2.110000 0.120000 2.230000 ( 2.607683)
|
247
|
+
n=18 4.410000 0.230000 4.640000 ( 5.223074)
|
248
|
+
n=19 9.240000 0.460000 9.700000 ( 10.995905)
|
249
|
+
n=20 19.280000 0.810000 20.090000 ( 23.152085)
|
250
|
+
|
251
|
+
JRuby 1.6.4 (ruby-1.8.7-p330) (2011-08-23 17ea768)
|
252
|
+
(OpenJDK 64-Bit Server VM 1.6.0_22) [linux-amd64-java]:
|
253
|
+
|
254
|
+
user system total real
|
255
|
+
n=10 0.539000 0.000000 0.539000 ( 0.369000)
|
256
|
+
n=11 0.609000 0.000000 0.609000 ( 0.609000)
|
257
|
+
n=12 0.490000 0.000000 0.490000 ( 0.490000)
|
258
|
+
n=13 0.701000 0.000000 0.701000 ( 0.701000)
|
259
|
+
n=14 1.013000 0.000000 1.013000 ( 1.013000)
|
260
|
+
n=15 0.729000 0.000000 0.729000 ( 0.729000)
|
261
|
+
n=16 0.306000 0.000000 0.306000 ( 0.306000)
|
262
|
+
n=17 0.601000 0.000000 0.601000 ( 0.601000)
|
263
|
+
n=18 1.291000 0.000000 1.291000 ( 1.291000)
|
264
|
+
n=19 2.591000 0.000000 2.591000 ( 2.591000)
|
265
|
+
n=20 5.573000 0.000000 5.573000 ( 5.573000)
|
266
|
+
|
267
|
+
Rubinius 2.0.0dev (1.8.7 5e67b11e yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]:
|
268
|
+
|
269
|
+
user system total real
|
270
|
+
n=10 0.050992 0.009999 0.060991 ( 0.196739)
|
271
|
+
n=11 0.088986 0.011998 0.100984 ( 0.295952)
|
272
|
+
n=12 0.141979 0.003999 0.145978 ( 0.632228)
|
273
|
+
n=13 0.248962 0.012998 0.261960 ( 0.939763)
|
274
|
+
n=14 0.482927 0.015998 0.498925 ( 0.562893)
|
275
|
+
n=15 0.988849 0.035994 1.024843 ( 1.134604)
|
276
|
+
n=16 2.034691 0.076988 2.111679 ( 2.342626)
|
277
|
+
n=17 4.188363 0.158976 4.347339 ( 4.882135)
|
278
|
+
n=18 8.595693 0.347947 8.943640 ( 10.191949)
|
279
|
+
n=19 17.662315 0.674898 18.337213 ( 20.490505)
|
280
|
+
n=20 36.434461 1.536766 37.971227 ( 42.821087)
|
281
|
+
|
data/ChangeLog.md
CHANGED
@@ -1,3 +1,45 @@
|
|
1
|
+
### 0.4.4 / 2022-01-16
|
2
|
+
|
3
|
+
* Fixed a bug in {Combinatorics::Choose.cardinality} for when `0`
|
4
|
+
is given and `0` was returned instead of `1`.
|
5
|
+
|
6
|
+
### 0.4.3 / 2012-05-28
|
7
|
+
|
8
|
+
* Fixed a typo in the gemspec, which incorrectly set
|
9
|
+
`required_rubygems_version` to the same value as `required_ruby_version`.
|
10
|
+
|
11
|
+
### 0.4.2 / 2012-05-27
|
12
|
+
|
13
|
+
* Avoid warnings on JRuby by checking for `::Generator` before
|
14
|
+
`::Enumerator::Generator` in `combinatorics/generator`.
|
15
|
+
* Raise a `NameError` when the `Generator` class cannot be found in
|
16
|
+
`combinatorics/generator`.
|
17
|
+
* Replaced ore-tasks with
|
18
|
+
[rubygems-tasks](https://github.com/postmodern/rubygems-tasks#readme).
|
19
|
+
|
20
|
+
### 0.4.1 / 2011-10-15
|
21
|
+
|
22
|
+
* Fixed a typo in the gemspec.
|
23
|
+
|
24
|
+
### 0.4.0 / 2011-10-15
|
25
|
+
|
26
|
+
* Added {Math.sigma}.
|
27
|
+
* Added {Math.pi}.
|
28
|
+
* Added {Math.factorial}.
|
29
|
+
* Added {Math.subfactorial}.
|
30
|
+
* Added {Combinatorics::CartesianProduct}.
|
31
|
+
* Added {Combinatorics::Choose}.
|
32
|
+
* Added {Combinatorics::Derange}.
|
33
|
+
* Added {Combinatorics::Permute}.
|
34
|
+
* Added {Combinatorics::PowerSet}.
|
35
|
+
|
36
|
+
### 0.3.1 / 2010-10-26
|
37
|
+
|
38
|
+
* Do not call `enum_for` on `Enumerator` objects passed to
|
39
|
+
{Array#comprehension}.
|
40
|
+
* Improved {Array#comprehension} benchmarks.
|
41
|
+
* Added [benchmark results] (see {file:Benchmarks.md}).
|
42
|
+
|
1
43
|
### 0.3.0 / 2010-10-17
|
2
44
|
|
3
45
|
* Added {Combinatorics::Generator} which auto-detects the `Generator`
|
data/Gemfile
ADDED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,96 +1,205 @@
|
|
1
1
|
# Combinatorics
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
[![CI](https://github.com/postmodern/combinatorics/actions/workflows/ruby.yml/badge.svg)](https://github.com/postmodern/combinatorics/actions/workflows/ruby.yml)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/postmodern/combinatorics.svg)](https://codeclimate.com/github/postmodern/combinatorics)
|
5
|
+
|
6
|
+
* [Source](https://github.com/postmodern/combinatorics)
|
7
|
+
* [Issues](https://github.com/postmodern/combinatorics/issues)
|
8
|
+
* [Documentation](http://rubydoc.info/gems/combinatorics)
|
6
9
|
|
7
10
|
## Description
|
8
11
|
|
9
12
|
A collection of modules and methods for performing
|
10
13
|
[Combinatoric](http://en.wikipedia.org/wiki/Combinatoric) calculations.
|
14
|
+
Methods are defined to compute power sets, Cartesian products, permutations,
|
15
|
+
combinations, and derangements.
|
16
|
+
|
17
|
+
Note: this includes k-combinations and k-permutations, i.e. only generating
|
18
|
+
resultant sets of a given size. Each set theory operation method supports block
|
19
|
+
arguments to allow continuous code execution during the combinatoric set
|
20
|
+
generation process. Each combinatoric operation implementation also supports a
|
21
|
+
`cardinality` method which determines the total number of subsets that will be
|
22
|
+
created beforehand (to aid in the avoidance of starting an operation which will
|
23
|
+
never complete due to exponential computational complexity.)
|
11
24
|
|
12
25
|
## Features
|
13
26
|
|
14
|
-
* Adds
|
15
|
-
*
|
16
|
-
*
|
27
|
+
* Adds Haskell/Python style list comprehensions via [Array#comprehension].
|
28
|
+
* Provides reusable Combinatorics Mixins:
|
29
|
+
* [Combinatorics::CartesianProduct]
|
30
|
+
* [Combinatorics::Choose]
|
31
|
+
* [Combinatorics::Derange]
|
32
|
+
* [Combinatorics::Permute]
|
33
|
+
* [Combinatorics::PowerSet]
|
34
|
+
* Adds Combinatorics core extension methods to {Array} and {Set}:
|
35
|
+
* [cartesian_product]
|
36
|
+
* [choose]
|
37
|
+
* [derange]
|
38
|
+
* [permute]
|
39
|
+
* [powerset]
|
40
|
+
* Adds additional core extension methods:
|
41
|
+
* [Range#&]
|
42
|
+
* [Range#upto]
|
43
|
+
* [Range#downto]
|
44
|
+
* [Math.sigma]
|
45
|
+
* [Math.pi]
|
46
|
+
* [Math.factorial]
|
47
|
+
* [Math.subfactorial]
|
48
|
+
|
49
|
+
[Array#comprehension]: https://rubydoc.info/gems/combinatorics/Array#comprehension-instance_method
|
50
|
+
[Combinatorics::CartesianProduct]: https://rubydoc.info/gems/combinatorics/Combinatorics/CartesianProduct
|
51
|
+
[Combinatorics::Choose]: https://rubydoc.info/gems/combinatorics/Combinatorics/Choose
|
52
|
+
[Combinatorics::Derange]: https://rubydoc.info/gems/combinatorics/Combinatorics/Derange
|
53
|
+
[Combinatorics::Permute]: https://rubydoc.info/gems/combinatorics/Combinatorics/Permute
|
54
|
+
[Combinatorics::PowerSet]: https://rubydoc.info/gems/combinatorics/Combinatorics/PowerSet
|
55
|
+
[cartesian_product]: https://rubydoc.info/gems/combinatorics/Combinatorics/CartesianProduct/Mixin#cartesian_product-instance_method
|
56
|
+
[choose]: https://rubydoc.info/gems/combinatorics/Combinatorics/Choose/Mixin#choose-instance_method
|
57
|
+
[derange]: https://rubydoc.info/gems/combinatorics/Combinatorics/Derange/Mixin#derange-instance_method
|
58
|
+
[permute]: https://rubydoc.info/gems/combinatorics/Combinatorics/Permute/Mixin#permute-instance_method
|
59
|
+
[powerset]: https://rubydoc.info/gems/combinatorics/Combinatorics/PowerSet/Mixin#powerset-instance_method
|
60
|
+
[Range#&]: https://rubydoc.info/gems/combinatorics/Range#&-instance_method
|
61
|
+
[Range#upto]: https://rubydoc.info/gems/combinatorics/Range#upto-instance_method
|
62
|
+
[Range#downto]: https://rubydoc.info/gems/combinatorics/Range#downto-instance_method
|
63
|
+
[Math.sigma]: https://rubydoc.info/gems/combinatorics/Math#sigma-class_method
|
64
|
+
[Math.pi]: https://rubydoc.info/gems/combinatorics/Math#pi-class_method
|
65
|
+
[Math.factorial]: https://rubydoc.info/gems/combinatorics/Math#factorial-class_method
|
66
|
+
[Math.subfactorial]: https://rubydoc.info/gems/combinatorics/Math#subfactorial-class_method
|
17
67
|
|
18
68
|
## Examples
|
19
69
|
|
20
|
-
Power-set
|
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
|
-
|
70
|
+
Power-set:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Set['ab', 'cd', 'ef'].powerset
|
74
|
+
# => [#<Set: {}>,
|
75
|
+
#<Set: {"ef"}>,
|
76
|
+
#<Set: {"cd"}>,
|
77
|
+
#<Set: {"cd", "ef"}>,
|
78
|
+
#<Set: {"ab"}>,
|
79
|
+
#<Set: {"ab", "ef"}>,
|
80
|
+
#<Set: {"ab", "cd"}>,
|
81
|
+
#<Set: {"ab", "cd", "ef"}>]
|
82
|
+
```
|
83
|
+
|
84
|
+
Cartesian products:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
require 'combinatorics/cartesian_product'
|
88
|
+
|
89
|
+
['a', 'b', 'c'].cartesian_product([0, 1, 2]).to_a
|
90
|
+
# => [["a", 0],
|
91
|
+
["b", 0],
|
92
|
+
["c", 0],
|
93
|
+
["a", 1],
|
94
|
+
["b", 1],
|
95
|
+
["c", 1],
|
96
|
+
["a", 2],
|
97
|
+
["b", 2],
|
98
|
+
["c", 2]]
|
99
|
+
```
|
100
|
+
|
101
|
+
k-combinations:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
require 'combinatorics/choose'
|
105
|
+
|
106
|
+
('a'..'f').to_a.choose(2).to_a
|
107
|
+
# => [["a", "b"],
|
108
|
+
["a", "c"],
|
109
|
+
["a", "d"],
|
110
|
+
["a", "e"],
|
111
|
+
["a", "f"],
|
112
|
+
["b", "c"],
|
113
|
+
["b", "d"],
|
114
|
+
["b", "e"],
|
115
|
+
["b", "f"],
|
116
|
+
["c", "d"],
|
117
|
+
["c", "e"],
|
118
|
+
["c", "f"],
|
119
|
+
["d", "e"],
|
120
|
+
["d", "f"],
|
121
|
+
["e", "f"]]
|
122
|
+
```
|
123
|
+
|
124
|
+
Derangements:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
require 'combinatorics/derange'
|
128
|
+
|
129
|
+
[:_, :q, :z, :x].derange.to_a
|
130
|
+
# => [[:q, :_, :x, :z],
|
131
|
+
[:q, :z, :x, :_],
|
132
|
+
[:q, :x, :_, :z],
|
133
|
+
[:z, :_, :x, :q],
|
134
|
+
[:z, :x, :_, :q],
|
135
|
+
[:z, :x, :q, :_],
|
136
|
+
[:x, :_, :q, :z],
|
137
|
+
[:x, :z, :_, :q],
|
138
|
+
[:x, :z, :q, :_]]
|
139
|
+
```
|
140
|
+
|
141
|
+
Permutation cardinality:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
require 'combinatorics/permutation'
|
145
|
+
|
146
|
+
Combinatorics::Permute.cardinality(128)
|
147
|
+
# => 8256
|
148
|
+
```
|
149
|
+
|
150
|
+
List comprehensions:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require 'combinatorics/list_comprehension'
|
154
|
+
|
155
|
+
[(0..10).step(2),('a'..'c')].comprehension.to_a
|
156
|
+
# => [[0, "a"],
|
157
|
+
[0, "b"],
|
158
|
+
[0, "c"],
|
159
|
+
[2, "a"],
|
160
|
+
[2, "b"],
|
161
|
+
[2, "c"],
|
162
|
+
[4, "a"],
|
163
|
+
[4, "b"],
|
164
|
+
[4, "c"],
|
165
|
+
[6, "a"],
|
166
|
+
[6, "b"],
|
167
|
+
[6, "c"],
|
168
|
+
[8, "a"],
|
169
|
+
[8, "b"],
|
170
|
+
[8, "c"],
|
171
|
+
[10, "a"],
|
172
|
+
[10, "b"],
|
173
|
+
[10, "c"]]
|
174
|
+
```
|
53
175
|
|
54
176
|
Find the intersecting sub-range between two ranges:
|
55
177
|
|
56
|
-
|
57
|
-
|
178
|
+
```ruby
|
179
|
+
(1..50) & (20..100)
|
180
|
+
# => (20..50)
|
181
|
+
```
|
58
182
|
|
59
183
|
Enumerate over every sub-range between two ranges:
|
60
184
|
|
61
|
-
|
62
|
-
|
63
|
-
|
185
|
+
```ruby
|
186
|
+
(1..5).upto(2..10).to_a
|
187
|
+
# => [1..5, 1..6, 1..7, 1..8, 1..9, 1..10,
|
188
|
+
2..5, 2..6, 2..7, 2..8, 2..9, 2..10]
|
189
|
+
```
|
64
190
|
|
65
|
-
|
191
|
+
## Requirements
|
66
192
|
|
67
|
-
|
68
|
-
|
69
|
-
[(0..10).step(2),('a'..'c')].comprehension.to_a
|
70
|
-
# => [[0, "a"],
|
71
|
-
[0, "b"],
|
72
|
-
[0, "c"],
|
73
|
-
[2, "a"],
|
74
|
-
[2, "b"],
|
75
|
-
[2, "c"],
|
76
|
-
[4, "a"],
|
77
|
-
[4, "b"],
|
78
|
-
[4, "c"],
|
79
|
-
[6, "a"],
|
80
|
-
[6, "b"],
|
81
|
-
[6, "c"],
|
82
|
-
[8, "a"],
|
83
|
-
[8, "b"],
|
84
|
-
[8, "c"],
|
85
|
-
[10, "a"],
|
86
|
-
[10, "b"],
|
87
|
-
[10, "c"]]
|
193
|
+
* [ruby](http://www.ruby-lang.org/) >= 1.8.7
|
88
194
|
|
89
195
|
## Install
|
90
196
|
|
91
|
-
|
197
|
+
```shell
|
198
|
+
$ gem install combinatorics
|
199
|
+
```
|
92
200
|
|
93
|
-
##
|
201
|
+
## Copyright
|
94
202
|
|
95
|
-
|
203
|
+
Copyright (c) 2010-2022 Hal Brodigan
|
96
204
|
|
205
|
+
See {file:LICENSE.txt} for license information.
|