combination-pairs 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/combination-pairs.gemspec +23 -0
- data/lib/combination/pairs.rb +72 -0
- data/lib/combination/pairs/version.rb +5 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Combination::Pairs
|
2
|
+
|
3
|
+
Input: K, N (where 0 < N < ∞, 0 < K < ∞, and K <= N)
|
4
|
+
Output: Number of possible equations of K numbers whose sum is N
|
5
|
+
|
6
|
+
Example Input:
|
7
|
+
N=10 K=3
|
8
|
+
Example Output:
|
9
|
+
Total unique equations = 8
|
10
|
+
1 + 1 + 8 = 10
|
11
|
+
1 + 2 + 7 = 10
|
12
|
+
1 + 3 + 6 = 10
|
13
|
+
1 + 4 + 5 = 10
|
14
|
+
2 + 2 + 6 = 10
|
15
|
+
2 + 3 + 5 = 10
|
16
|
+
2 + 4 + 4 = 10
|
17
|
+
3 + 3 + 4 = 10
|
18
|
+
For reference, N=100, K=3 should have a result of 833 unique sets
|
19
|
+
|
20
|
+
Its performance is better than the repeated-combination of array module but need to be optimized. So please contribute in it make it more optimized.
|
21
|
+
|
22
|
+
Now it is working for k =4 and n<1000 and take time for k > 4 so please contribute to make it more effective.
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
gem 'combination-pairs'
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install combination-pairs
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
There are basic two varibales n and k
|
41
|
+
n should greater than k
|
42
|
+
|
43
|
+
params = {:n =>100, :k =>4}
|
44
|
+
c = Combination::Pairs::Equation.new(params)
|
45
|
+
c.create_equations
|
46
|
+
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'combination/pairs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "combination-pairs"
|
8
|
+
spec.version = Combination::Pairs::VERSION
|
9
|
+
spec.authors = ["Tarun"]
|
10
|
+
spec.email = ["t.garg28@gmail.com"]
|
11
|
+
spec.description = %q{K, N (where 0 < N < ∞, 0 < K < ∞, and K <= N)}
|
12
|
+
spec.summary = %q{Number of possible equations of K numbers whose sum is N}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "combination/pairs/version"
|
2
|
+
|
3
|
+
module Combination
|
4
|
+
module Pairs
|
5
|
+
class Equation
|
6
|
+
def initialize(params)
|
7
|
+
@arr=[]
|
8
|
+
@n = params[:n]
|
9
|
+
@k = params[:k]
|
10
|
+
end
|
11
|
+
|
12
|
+
#To create possible equations
|
13
|
+
def create_equations
|
14
|
+
return "Please Enter value of n and k" if @k.blank? && @n.blank?
|
15
|
+
begin
|
16
|
+
Integer(@k)
|
17
|
+
rescue
|
18
|
+
return "Error: Please enter any +ve integer value of k"
|
19
|
+
end
|
20
|
+
begin
|
21
|
+
Integer(@n)
|
22
|
+
rescue
|
23
|
+
return "Error: Please enter any +ve integer value of n"
|
24
|
+
end
|
25
|
+
return "Please enter k < n" if @n < @k
|
26
|
+
create_equations_sum
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_equations_sum
|
30
|
+
aar = []
|
31
|
+
@arr = []
|
32
|
+
@list_elements=(1..@n).to_a
|
33
|
+
(1..@k-1).each do |i|
|
34
|
+
aar << [*0..@n-1]
|
35
|
+
end
|
36
|
+
traverse([], aar, 0)
|
37
|
+
return @arr.uniq #return result
|
38
|
+
end
|
39
|
+
|
40
|
+
#To check sum
|
41
|
+
def generate_sum(*args)
|
42
|
+
new_elements = []
|
43
|
+
total= 0
|
44
|
+
args.flatten.each do |arg|
|
45
|
+
total += @list_elements[arg]
|
46
|
+
new_elements << @list_elements[arg]
|
47
|
+
end
|
48
|
+
if total < @n
|
49
|
+
new_elements << @n - total
|
50
|
+
@arr << new_elements.sort
|
51
|
+
else
|
52
|
+
return
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def innerloop(arrayOfCurrentValues)
|
57
|
+
generate_sum(arrayOfCurrentValues)
|
58
|
+
end
|
59
|
+
|
60
|
+
#Recursive method to create dynamic nested loops.
|
61
|
+
def traverse(accumulated,params, index)
|
62
|
+
if (index==params.size)
|
63
|
+
return innerloop(accumulated)
|
64
|
+
end
|
65
|
+
currentParam = params[index]
|
66
|
+
currentParam.each do |currentElementOfCurrentParam|
|
67
|
+
traverse(accumulated+[currentElementOfCurrentParam],params, index+1)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: combination-pairs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tarun
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-05-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: K, N (where 0 < N < ∞, 0 < K < ∞, and K <= N)
|
47
|
+
email:
|
48
|
+
- t.garg28@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- combination-pairs.gemspec
|
59
|
+
- lib/combination/pairs.rb
|
60
|
+
- lib/combination/pairs/version.rb
|
61
|
+
homepage: ''
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.27
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Number of possible equations of K numbers whose sum is N
|
86
|
+
test_files: []
|