combination_generator 0.1.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/README +17 -0
- data/combination_generator.gemspec +15 -0
- data/lib/combination_generator.rb +104 -0
- data/test/test_combination_generator.rb +12 -0
- metadata +70 -0
data/README
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
= CombinationGenerator
|
|
2
|
+
|
|
3
|
+
== Install
|
|
4
|
+
$ sudo gem install combination_generator
|
|
5
|
+
|
|
6
|
+
== Usage
|
|
7
|
+
require 'rubygems'
|
|
8
|
+
require 'combination_generator'
|
|
9
|
+
CombinationGenerator.new(2, ["r", "b", "g"]).each do |element|
|
|
10
|
+
p element
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
output -- ["r", "b"] ["r", "g"] ["b", "g"]
|
|
14
|
+
Copyright (c) 2010 MIT-LICENSE
|
|
15
|
+
Author : Wayne Deng
|
|
16
|
+
Web : http://blog.waynedeng.com
|
|
17
|
+
Email : wayne.deng.cn(AT).com
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = %q{combination_generator}
|
|
3
|
+
s.version = "0.1.1"
|
|
4
|
+
|
|
5
|
+
s.authors = ["Wayne Deng"]
|
|
6
|
+
s.date = %q{2010-09-20}
|
|
7
|
+
s.platform = Gem::Platform::RUBY
|
|
8
|
+
s.summary = "generate combination list in Ruby"
|
|
9
|
+
s.description = "Combination Generator - generate combination list in Ruby"
|
|
10
|
+
s.email = %q{wayne.deng.cn@gmail.com}
|
|
11
|
+
s.homepage = %q{http://blog.waynedeng.com}
|
|
12
|
+
s.files = ["test/test_combination_generator.rb", "README", "lib/combination_generator.rb", "combination_generator.gemspec"]
|
|
13
|
+
s.require_paths = ["lib"]
|
|
14
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Combination Generator", "--main", "README"]
|
|
15
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#= CombinationGenerator
|
|
2
|
+
#
|
|
3
|
+
#== Install
|
|
4
|
+
# $ sudo gem install combination_generator
|
|
5
|
+
#
|
|
6
|
+
#== Usage
|
|
7
|
+
# require 'rubygems'
|
|
8
|
+
# require 'combination_generator'
|
|
9
|
+
# CombinationGenerator.new(2, ["r", "b", "g"]).each do |element|
|
|
10
|
+
# p element
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# # output -- ["r", "b"] ["r", "g"] ["b", "g"]
|
|
14
|
+
#Copyright (c) 2010 MIT-LICENSE
|
|
15
|
+
#Author : Wayne Deng
|
|
16
|
+
#Web : http://blog.waynedeng.com
|
|
17
|
+
#Email : wayne.deng.cn(AT).com
|
|
18
|
+
|
|
19
|
+
class CombinationGenerator
|
|
20
|
+
attr_accessor :size, :elements, :comb_index
|
|
21
|
+
|
|
22
|
+
class CombinationIndex
|
|
23
|
+
attr_accessor :size, :element_size, :index_ary
|
|
24
|
+
|
|
25
|
+
def initialize(picking_size, element_size)
|
|
26
|
+
self.element_size = element_size
|
|
27
|
+
self.size = picking_size
|
|
28
|
+
self.index_ary = [1] * self.size + [0] * (element_size - self.size)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def each (&block)
|
|
32
|
+
yield self.index_ary
|
|
33
|
+
while self.move_next
|
|
34
|
+
yield self.index_ary
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def each_with_index (&block)
|
|
39
|
+
i = 0
|
|
40
|
+
yield self.index_ary, i
|
|
41
|
+
while self.move_next
|
|
42
|
+
i = i + 1
|
|
43
|
+
yield self.index_ary, i
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def move_next
|
|
48
|
+
(1..self.size).each do |i|
|
|
49
|
+
return true if self.move_i_next(i)
|
|
50
|
+
end
|
|
51
|
+
return false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
#the index of last ith 1
|
|
55
|
+
def last_1_index(num = 1)
|
|
56
|
+
count = 0
|
|
57
|
+
self.index_ary.reverse.each_with_index do |e, i|
|
|
58
|
+
count = count + 1 if e==1
|
|
59
|
+
return (self.index_ary.size - i - 1) if count>=num
|
|
60
|
+
end
|
|
61
|
+
return -1
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def move_i_next(num = 1)
|
|
65
|
+
index = self.last_1_index(num)
|
|
66
|
+
#if already move to the last position
|
|
67
|
+
return nil if index == -1 or index >= self.index_ary.size - 1
|
|
68
|
+
#if the element in next position is 1
|
|
69
|
+
return nil if self.index_ary[index + 1]==1
|
|
70
|
+
|
|
71
|
+
self.index_ary[index] = 0
|
|
72
|
+
self.index_ary[index + 1] = 1
|
|
73
|
+
return true
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def initialize(picking_size, elements)
|
|
78
|
+
self.elements = elements
|
|
79
|
+
self.size = picking_size
|
|
80
|
+
self.comb_index = CombinationIndex.new(self.size, elements.size)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def each (&block)
|
|
84
|
+
self.comb_index.each do |ary|
|
|
85
|
+
yield self.selected_elements(ary)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def each_with_index (&block)
|
|
90
|
+
self.comb_index.each_with_index do |ary, i|
|
|
91
|
+
yield self.selected_elements(ary), i
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
protected
|
|
96
|
+
def selected_elements(ary)
|
|
97
|
+
_elements = []
|
|
98
|
+
ary.each_with_index do |e, i|
|
|
99
|
+
_elements << self.elements[i] if e==1
|
|
100
|
+
end
|
|
101
|
+
_elements
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
require File.join(File.dirname(__FILE__),"..","lib","combination_generator.rb")
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
|
|
5
|
+
class TestCombinationGenerator < Test::Unit::TestCase
|
|
6
|
+
def test_each
|
|
7
|
+
ary = []
|
|
8
|
+
CombinationGenerator.new(2, [1,2,3]).each{|ai| ary << ai}
|
|
9
|
+
assert_equal ary, [[1,2],[1,3],[2,3]]
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: combination_generator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 1
|
|
8
|
+
- 1
|
|
9
|
+
version: 0.1.1
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Wayne Deng
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2010-09-20 00:00:00 +08:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: Combination Generator - generate combination list in Ruby
|
|
22
|
+
email: wayne.deng.cn@gmail.com
|
|
23
|
+
executables: []
|
|
24
|
+
|
|
25
|
+
extensions: []
|
|
26
|
+
|
|
27
|
+
extra_rdoc_files: []
|
|
28
|
+
|
|
29
|
+
files:
|
|
30
|
+
- test/test_combination_generator.rb
|
|
31
|
+
- README
|
|
32
|
+
- lib/combination_generator.rb
|
|
33
|
+
- combination_generator.gemspec
|
|
34
|
+
has_rdoc: true
|
|
35
|
+
homepage: http://blog.waynedeng.com
|
|
36
|
+
licenses: []
|
|
37
|
+
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options:
|
|
40
|
+
- --line-numbers
|
|
41
|
+
- --inline-source
|
|
42
|
+
- --title
|
|
43
|
+
- Combination Generator
|
|
44
|
+
- --main
|
|
45
|
+
- README
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
segments:
|
|
53
|
+
- 0
|
|
54
|
+
version: "0"
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
segments:
|
|
60
|
+
- 0
|
|
61
|
+
version: "0"
|
|
62
|
+
requirements: []
|
|
63
|
+
|
|
64
|
+
rubyforge_project:
|
|
65
|
+
rubygems_version: 1.3.6
|
|
66
|
+
signing_key:
|
|
67
|
+
specification_version: 3
|
|
68
|
+
summary: generate combination list in Ruby
|
|
69
|
+
test_files: []
|
|
70
|
+
|