partitions 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -0
- data/lib/partitions/set_partitions.rb +99 -31
- data/lib/partitions/version.rb +1 -1
- data/partitions.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2aa22121b5627f30e18b9a2740d3cac3b8f33ecb
|
4
|
+
data.tar.gz: f1857f98aaf24b9f6db43cb62ddbb5203b4c1f14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96c4c851431664fbebeee2622cf118ab78b4400ae91788e821ef8dce4dfbfcfc8635c1a0f1e731dc9cb8186f6956676cfa7f347e4ee5c94c041a24227342a3be
|
7
|
+
data.tar.gz: 2650403dca28ff72cf42662491ad0a021b357f11829fe0a4b46020923e741dc69411c663c4a236a807d0ed3878a73807a82892ce034e375eaa66f24dfeefb4e5
|
data/README.md
CHANGED
@@ -92,6 +92,26 @@ Or install it yourself as:
|
|
92
92
|
irb(main):004:0> sp.count
|
93
93
|
=> 4140
|
94
94
|
|
95
|
+
irb(main):005:0> sp = Partitions::SetPartitions.new(6,5)
|
96
|
+
=> #<Partitions::SetPartitions:0x007fd9c284def8 @n=6, @k=[0, 0, 1, 2, 3, 4], @m=[0, 0, 1, 2, 3, 4], @p=5, @size=5>
|
97
|
+
irb(main):006:0> sp.each_partition{|partition| p partition};nil
|
98
|
+
[0, 0, 1, 2, 3, 4]
|
99
|
+
[0, 1, 0, 2, 3, 4]
|
100
|
+
[0, 1, 1, 2, 3, 4]
|
101
|
+
[0, 1, 2, 0, 3, 4]
|
102
|
+
[0, 1, 2, 1, 3, 4]
|
103
|
+
[0, 1, 2, 2, 3, 4]
|
104
|
+
[0, 1, 2, 3, 0, 4]
|
105
|
+
[0, 1, 2, 3, 1, 4]
|
106
|
+
[0, 1, 2, 3, 2, 4]
|
107
|
+
[0, 1, 2, 3, 3, 4]
|
108
|
+
[0, 1, 2, 3, 4, 0]
|
109
|
+
[0, 1, 2, 3, 4, 1]
|
110
|
+
[0, 1, 2, 3, 4, 2]
|
111
|
+
[0, 1, 2, 3, 4, 3]
|
112
|
+
[0, 1, 2, 3, 4, 4]
|
113
|
+
=> nil
|
114
|
+
|
95
115
|
##References
|
96
116
|
###Integer Partitions
|
97
117
|
---------------------------
|
@@ -1,47 +1,93 @@
|
|
1
1
|
module Partitions
|
2
2
|
class SetPartitions
|
3
|
-
|
3
|
+
attr_reader :size
|
4
4
|
|
5
5
|
def initialize(n, p=nil)
|
6
6
|
@n = n
|
7
7
|
@k = Array.new(n, 0)
|
8
8
|
@m = Array.new(n, 0)
|
9
9
|
@p = p
|
10
|
-
|
10
|
+
pinitialize
|
11
|
+
@size = partition_size
|
11
12
|
end
|
12
13
|
|
13
14
|
def next_partition
|
14
|
-
|
15
|
-
|
16
|
-
@k[i]
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
if @p.nil?
|
16
|
+
(@n - 1).downto(1) do |i|
|
17
|
+
if @k[i] <= @m[i - 1]
|
18
|
+
@k[i] = @k[i] + 1
|
19
|
+
@m[i] = max(@m[i], @k[i])
|
20
|
+
j = i + 1
|
21
|
+
while j <= (n - 1) do
|
22
|
+
@k[j] = @k[0]
|
23
|
+
@m[j] = @m[i]
|
24
|
+
j = j + 1
|
25
|
+
end
|
26
|
+
@size = partition_size
|
27
|
+
return @k.clone
|
28
|
+
end
|
29
|
+
end
|
30
|
+
else
|
31
|
+
p = partition_size
|
32
|
+
(@n - 1).downto(1) do |i|
|
33
|
+
if (@k[i] < (p - 1)) && (@k[i] <= @m[i - 1])
|
34
|
+
@k[i] = @k[i] + 1
|
35
|
+
@m[i] = max(@m[i], @k[i])
|
36
|
+
j = i + 1
|
37
|
+
while j <= (@n - (p - @m[i])) do
|
38
|
+
@k[j] = 0
|
39
|
+
@m[j] = @m[i]
|
40
|
+
j = j + 1
|
41
|
+
end
|
42
|
+
j = @n - (p - @m[i]) + 1
|
43
|
+
while j <= (n - 1) do
|
44
|
+
@k[j] = @m[j] = p - (@n - j)
|
45
|
+
j = j + 1
|
46
|
+
end
|
47
|
+
@size = partition_size
|
48
|
+
return @k.clone
|
23
49
|
end
|
24
|
-
@size = partition_size
|
25
|
-
return @k
|
26
50
|
end
|
27
51
|
end
|
28
52
|
|
29
53
|
@size = nil
|
30
|
-
return
|
54
|
+
return
|
31
55
|
end
|
32
56
|
|
33
57
|
def previous_partition
|
34
|
-
|
35
|
-
|
36
|
-
@k[i]
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
58
|
+
if @p.nil?
|
59
|
+
(@n - 1).downto(1) do |i|
|
60
|
+
if @k[i] > @k[0]
|
61
|
+
@k[i] = @k[i] - 1
|
62
|
+
@m[i] = @m[i - 1]
|
63
|
+
j = i + 1
|
64
|
+
while j <= n - 1 do
|
65
|
+
@k[j] = @m[j] = @m[i] + j - i;
|
66
|
+
j = j + 1
|
67
|
+
end
|
68
|
+
@size = partition_size
|
69
|
+
return @k.clone
|
70
|
+
end
|
71
|
+
end
|
72
|
+
else
|
73
|
+
p = partition_size
|
74
|
+
(@n - 1).downto(1).each do |i|
|
75
|
+
if (@k[i] > 0) && ((p - @m[i - 1]) <= (n - i))
|
76
|
+
@k[i] = @k[i] - 1
|
77
|
+
@m[i] = @m[i - 1]
|
78
|
+
j = i + 1
|
79
|
+
while j <= (i + (p - @m[i]) - 1) do
|
80
|
+
@k[j] = @m[j] = @m[i] + j - i
|
81
|
+
j = j + 1
|
82
|
+
end
|
83
|
+
j = i + (p - @m[i])
|
84
|
+
while j <= (@n - 1) do
|
85
|
+
@k[j] = @m[j] = p - 1
|
86
|
+
j = j + 1
|
87
|
+
end
|
88
|
+
@size = partition_size
|
89
|
+
return @k.clone
|
42
90
|
end
|
43
|
-
@size = partition_size
|
44
|
-
return @k
|
45
91
|
end
|
46
92
|
end
|
47
93
|
|
@@ -54,23 +100,31 @@ module Partitions
|
|
54
100
|
raise ArgumentError, "Missing block"
|
55
101
|
end
|
56
102
|
reinitialize
|
57
|
-
yield @k
|
103
|
+
yield @k.clone
|
58
104
|
(count - 1).times do
|
59
|
-
|
105
|
+
partition = next_partition
|
106
|
+
yield partition
|
60
107
|
end
|
61
108
|
reinitialize
|
62
109
|
end
|
63
110
|
|
64
111
|
def count
|
65
|
-
|
66
|
-
|
67
|
-
|
112
|
+
if @p.nil?
|
113
|
+
sum = 0
|
114
|
+
(1..n).each do |k|
|
115
|
+
sum += sterling_second(@n, k)
|
116
|
+
end
|
117
|
+
return sum
|
118
|
+
else
|
119
|
+
sum = sterling_second(@n, @p)
|
68
120
|
end
|
69
|
-
return sum
|
70
121
|
end
|
71
122
|
|
72
123
|
private
|
73
124
|
|
125
|
+
attr_accessor :k, :m, :n
|
126
|
+
attr_writer :size
|
127
|
+
|
74
128
|
def sterling_second(n, k)
|
75
129
|
if k == 1 || k == n
|
76
130
|
return 1
|
@@ -86,7 +140,21 @@ module Partitions
|
|
86
140
|
def reinitialize
|
87
141
|
@k = Array.new(n, 0)
|
88
142
|
@m = Array.new(n, 0)
|
89
|
-
|
143
|
+
pinitialize
|
144
|
+
@size = partition_size
|
145
|
+
end
|
146
|
+
|
147
|
+
def pinitialize
|
148
|
+
unless @p.nil?
|
149
|
+
(0..(@n - @p)).each do |i|
|
150
|
+
@k[i] = @m[i] = 0
|
151
|
+
end
|
152
|
+
i = (@n - @p + 1)
|
153
|
+
while i <= (@n - 1) do
|
154
|
+
@k[i] = @m[i] = i - (@n - @p)
|
155
|
+
i = i + 1
|
156
|
+
end
|
157
|
+
end
|
90
158
|
end
|
91
159
|
|
92
160
|
def max a, b
|
data/lib/partitions/version.rb
CHANGED
data/partitions.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "partitions"
|
8
8
|
spec.version = Partitions::VERSION
|
9
9
|
spec.authors = ["NagaChaitanya Vellanki"]
|
10
|
-
spec.email = ["
|
10
|
+
spec.email = ["nagachaitanya.vellanki.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{a rubygem to generate partitions.}
|
13
13
|
spec.description = %q{Generate integer, set and multiset partitions}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: partitions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- NagaChaitanya Vellanki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
version: '0.10'
|
69
69
|
description: Generate integer, set and multiset partitions
|
70
70
|
email:
|
71
|
-
-
|
71
|
+
- nagachaitanya.vellanki.com
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|