partitions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2decbf60e2bc3cd7377d2d42b003897d7e167c2f
4
+ data.tar.gz: 4eee0fbaa035a56b04f862c4207442f365256a73
5
+ SHA512:
6
+ metadata.gz: 1a564fa6fe63af32ae7aed33cec7a3c7ff3f878bfe62d24f1cca23a2b1e9849e21ad31d990cd96259880e2638e3f1b50725de6acbea5c1ce939f1dafe46b082f
7
+ data.tar.gz: 354d064c68d4a5c9b9ac944dd0613ed54c21ec2f6bfe7c278f1b599fedf5576b9d0b082de3acd104509528b44ca2bffa3016e6213e8e862c700aee3a678213d1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /coverage/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in partitions.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2015, NagaChaitanya Vellanki <me@chaitanyavellanki.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Partitions
2
+
3
+ Partitions is a rubygem to list integer, set and multiset partitions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'partitions'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install partitions
20
+
21
+ ## Usage
22
+ >> require 'partitions' #=> true
23
+ >> 5.partitions #=> nil
24
+ [1, 1, 1, 1, 1]
25
+ [1, 1, 1, 2]
26
+ [1, 1, 3]
27
+ [1, 2, 2]
28
+ [1, 4]
29
+ [2, 3]
30
+ [5]
31
+
32
+ >> 5.partitions{|partition| p partition};nil #=> nil
33
+ [1, 1, 1, 1, 1]
34
+ [1, 1, 1, 2]
35
+ [1, 1, 3]
36
+ [1, 2, 2]
37
+ [1, 4]
38
+ [2, 3]
39
+ [5]
40
+
41
+
42
+ irb(main):001:0> sp = Partitions::SetPartitions.new(4)
43
+ => #<Partitions::SetPartitions:0x007fedab516f58 @n=4, @k=[0, 0, 0, 0], @m=[0, 0, 0, 0], @p=nil, @size=0>
44
+ irb(main):002:0> sp.each_partition{|partition| p partition};nil
45
+ [0, 0, 0, 0]
46
+ [0, 0, 0, 1]
47
+ [0, 0, 1, 0]
48
+ [0, 0, 1, 1]
49
+ [0, 0, 1, 2]
50
+ [0, 1, 0, 0]
51
+ [0, 1, 0, 1]
52
+ [0, 1, 0, 2]
53
+ [0, 1, 1, 0]
54
+ [0, 1, 1, 1]
55
+ [0, 1, 1, 2]
56
+ [0, 1, 2, 0]
57
+ [0, 1, 2, 1]
58
+ [0, 1, 2, 2]
59
+ [0, 1, 2, 3]
60
+ => nil
61
+
62
+ irb(main):003:0> sp = Partitions::SetPartitions.new(3)
63
+ => #<Partitions::SetPartitions:0x007fedab4fea48 @n=3, @k=[0, 0, 0], @m=[0, 0, 0], @p=nil, @size=0>
64
+ irb(main):004:0> sp.next_partition
65
+ => [0, 0, 1]
66
+ irb(main):005:0> sp.next_partition
67
+ => [0, 1, 0]
68
+ irb(main):006:0> sp.next_partition
69
+ => [0, 1, 1]
70
+ irb(main):007:0> sp.next_partition
71
+ => [0, 1, 2]
72
+ irb(main):008:0> sp.next_partition
73
+ => nil
74
+ irb(main):009:0> sp.previous_partition
75
+ => [0, 1, 1]
76
+ irb(main):010:0> sp.previous_partition
77
+ => [0, 1, 0]
78
+ irb(main):011:0> sp.previous_partition
79
+ => [0, 0, 1]
80
+ irb(main):012:0> sp.previous_partition
81
+ => [0, 0, 0]
82
+ irb(main):013:0> sp.previous_partition
83
+ => nil
84
+
85
+ irb(main):001:0> sp = Partitions::SetPartitions.new(5)
86
+ => #<Partitions::SetPartitions:0x007f903c426a70 @n=5, @k=[0, 0, 0, 0, 0], @m=[0, 0, 0, 0, 0], @p=nil, @size=0>
87
+ irb(main):002:0> sp.count
88
+ => 52
89
+
90
+ irb(main):003:0> sp = Partitions::SetPartitions.new(8)
91
+ => #<Partitions::SetPartitions:0x007f903c417458 @n=8, @k=[0, 0, 0, 0, 0, 0, 0, 0], @m=[0, 0, 0, 0, 0, 0, 0, 0], @p=nil, @size=0>
92
+ irb(main):004:0> sp.count
93
+ => 4140
94
+
95
+ ##References
96
+ ###Integer Partitions
97
+ ---------------------------
98
+ * [Generating All Partitions: A Comparison Of Two Encodings Jerome Kelleher, Barry O'Sullivan](http://arxiv.org/abs/0909.2331v2)
99
+ * http://jeromekelleher.net/partitions.php
100
+
101
+ ###Set Partitions
102
+ ---------------------------
103
+ [Orlov, M.: Efficient generation of set partitions. Tech. rep., Engineering and Computer Sciences, University of Ulm (2002)](http://www.informatik.uni-ulm.de/ni/Lehre/WS03/DMM/Software/partitions.pdf)
104
+
105
+ ###Stirling Number of the Second Kind
106
+ -----------------------------------------
107
+ http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
114
+
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it ( https://github.com/chaitanyav/partitions/)
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "partitions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,39 @@
1
+ module Partitions
2
+
3
+ module IntegerPartitions
4
+ def partitions
5
+ n = self
6
+ if n < 0
7
+ raise ArgumentError, "n should be greater than or equal to 0"
8
+ end
9
+
10
+ a = Array.new(n + 1, 0)
11
+ k = 2
12
+ a[1] = 0
13
+ a[2] = n
14
+ while k != 1 do
15
+ y = a[k] - 1
16
+ k = k - 1
17
+ x = a[k] + 1
18
+
19
+ while x <= y do
20
+ a[k] = x
21
+ y = y - x
22
+ k = k + 1
23
+ end
24
+
25
+ a[k] = x + y
26
+
27
+ if block_given?
28
+ yield a.values_at(1..k)
29
+ else
30
+ p a.values_at(1..k)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ class Integer
38
+ include Partitions::IntegerPartitions
39
+ end
@@ -0,0 +1,97 @@
1
+ module Partitions
2
+ class SetPartitions
3
+ attr_accessor :k, :m, :n, :size
4
+
5
+ def initialize(n, p=nil)
6
+ @n = n
7
+ @k = Array.new(n, 0)
8
+ @m = Array.new(n, 0)
9
+ @p = p
10
+ @size = 0
11
+ end
12
+
13
+ def next_partition
14
+ (@n - 1).downto(1) do |i|
15
+ if @k[i] <= @m[i - 1]
16
+ @k[i] = @k[i] + 1
17
+ @m[i] = max(@m[i], @k[i])
18
+ j = i + 1
19
+ while j <= (n - 1) do
20
+ @k[j] = @k[0]
21
+ @m[j] = @m[i]
22
+ j = j + 1
23
+ end
24
+ @size = partition_size
25
+ return @k
26
+ end
27
+ end
28
+
29
+ @size = nil
30
+ return
31
+ end
32
+
33
+ def previous_partition
34
+ (@n - 1).downto(1) do |i|
35
+ if @k[i] > @k[0]
36
+ @k[i] = @k[i] - 1
37
+ @m[i] = @m[i - 1]
38
+ j = i + 1
39
+ while j <= n - 1 do
40
+ @k[j] = @m[j] = @m[i] + j - i;
41
+ j = j + 1
42
+ end
43
+ @size = partition_size
44
+ return @k
45
+ end
46
+ end
47
+
48
+ @size = nil
49
+ return
50
+ end
51
+
52
+ def each_partition
53
+ unless block_given?
54
+ raise ArgumentError, "Missing block"
55
+ end
56
+ reinitialize
57
+ yield @k
58
+ (count - 1).times do
59
+ yield next_partition
60
+ end
61
+ reinitialize
62
+ end
63
+
64
+ def count
65
+ sum = 0
66
+ (1..n).each do |k|
67
+ sum += sterling_second(@n, k)
68
+ end
69
+ return sum
70
+ end
71
+
72
+ private
73
+
74
+ def sterling_second(n, k)
75
+ if k == 1 || k == n
76
+ return 1
77
+ else
78
+ return sterling_second(n - 1, k - 1) + k * sterling_second(n - 1, k)
79
+ end
80
+ end
81
+
82
+ def partition_size
83
+ @m[n - 1] - @m[0] + 1
84
+ end
85
+
86
+ def reinitialize
87
+ @k = Array.new(n, 0)
88
+ @m = Array.new(n, 0)
89
+ @size = 0
90
+ end
91
+
92
+ def max a, b
93
+ return (a < b) ? b : a
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Partitions
2
+ VERSION = "0.1.0"
3
+ end
data/lib/partitions.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "partitions/version"
2
+ require "partitions/set_partitions"
3
+ require "partitions/integer_partitions"
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'partitions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "partitions"
8
+ spec.version = Partitions::VERSION
9
+ spec.authors = ["NagaChaitanya Vellanki"]
10
+ spec.email = ["me@chaitanyavellanki.com"]
11
+
12
+ spec.summary = %q{a rubygem to generate partitions.}
13
+ spec.description = %q{Generate integer, set and multiset partitions}
14
+ spec.homepage = "https://github.com/chaitanyav/partitions"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.9"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.6"
32
+ spec.add_development_dependency "simplecov", "~> 0.10"
33
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: partitions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - NagaChaitanya Vellanki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description: Generate integer, set and multiset partitions
70
+ email:
71
+ - me@chaitanyavellanki.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/partitions.rb
86
+ - lib/partitions/.DS_Store
87
+ - lib/partitions/integer_partitions.rb
88
+ - lib/partitions/set_partitions.rb
89
+ - lib/partitions/version.rb
90
+ - partitions.gemspec
91
+ homepage: https://github.com/chaitanyav/partitions
92
+ licenses: []
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: a rubygem to generate partitions.
115
+ test_files: []