combination_generator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{combination_generator}
3
- s.version = "0.1.1"
3
+ s.version = "0.1.2"
4
4
 
5
5
  s.authors = ["Wayne Deng"]
6
- s.date = %q{2010-09-20}
6
+ s.date = %q{2011-03-29}
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.summary = "generate combination list in Ruby"
9
9
  s.description = "Combination Generator - generate combination list in Ruby"
@@ -43,34 +43,25 @@ class CombinationGenerator
43
43
  yield self.index_ary, i
44
44
  end
45
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
46
+
47
+ def move_1_to_left(end_i)
48
+ return if end_i<1
49
+ count_1 = self.index_ary[0..end_i].find_all{|e| e==1}.size
50
+ self.index_ary[0..end_i] = [1] * count_1 + [0] * (end_i - count_1 + 1)
52
51
  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
52
+
53
+ def move_next
54
+ #scan from left, if has 1 0 then change to 0 1 and move all the 1 in the left move to the most left
55
+ self.index_ary.each_with_index do |e, i|
56
+ if i<self.index_ary.size and self.index_ary[i]==1 and self.index_ary[i+1]==0
57
+ self.index_ary[i] = 0
58
+ self.index_ary[i + 1] = 1
59
+ self.move_1_to_left(i - 1)
60
+ return true
61
+ end
60
62
  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
63
+
64
+ return nil
74
65
  end
75
66
  end
76
67
 
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combination_generator
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 31
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Wayne Deng
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-09-20 00:00:00 +08:00
18
+ date: 2011-03-29 00:00:00 +08:00
18
19
  default_executable:
19
20
  dependencies: []
20
21
 
@@ -46,23 +47,27 @@ rdoc_options:
46
47
  require_paths:
47
48
  - lib
48
49
  required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
49
51
  requirements:
50
52
  - - ">="
51
53
  - !ruby/object:Gem::Version
54
+ hash: 3
52
55
  segments:
53
56
  - 0
54
57
  version: "0"
55
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
56
60
  requirements:
57
61
  - - ">="
58
62
  - !ruby/object:Gem::Version
63
+ hash: 3
59
64
  segments:
60
65
  - 0
61
66
  version: "0"
62
67
  requirements: []
63
68
 
64
69
  rubyforge_project:
65
- rubygems_version: 1.3.6
70
+ rubygems_version: 1.5.2
66
71
  signing_key:
67
72
  specification_version: 3
68
73
  summary: generate combination list in Ruby