frequency_enumerator 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,7 +26,7 @@ You might discover, that in fact, almost no one drinks tea and everyone loves bi
26
26
  { :tea => 25, :coffee => 60, :biscuit => 115 }
27
27
  ```
28
28
 
29
- It'd be nice if we could brute-force the problem, but be more intelligent about the order in which we do so. We should use make use of our valuable, newfound knowledge.
29
+ It'd be nice if we could brute-force the problem, but be more intelligent about the order in which we do so. We should make use of our valuable, newfound knowledge.
30
30
 
31
31
  And that's exactly what Frequency Enumerator does. (I got there in the end!)
32
32
 
@@ -50,7 +50,7 @@ end
50
50
  The first 10 attempts yielded to the block are:
51
51
 
52
52
  ```ruby
53
- { :tea => 1, :coffee => 1, :biscuit => 1 }
53
+ { :tea => 0, :coffee => 0, :biscuit => 0 }
54
54
  { :tea => 0, :coffee => 0, :biscuit => 1 }
55
55
  { :tea => 0, :coffee => 0, :biscuit => 2 }
56
56
  { :tea => 0, :coffee => 0, :biscuit => 3 }
@@ -62,7 +62,7 @@ The first 10 attempts yielded to the block are:
62
62
  { :tea => 0, :coffee => 0, :biscuit => 5 }
63
63
  ```
64
64
 
65
- As you can see, most of attempts change the number of biscuits, whilst we haven't even explored the possibility that tea might be in the solution yet.
65
+ As you can see, most of the attempts change the number of biscuits, whilst we haven't even explored the possibility that tea might be in the solution yet.
66
66
 
67
67
  ## Limit
68
68
 
@@ -85,6 +85,13 @@ You can set 'from' and 'to' to explore different portions of the search space:
85
85
 
86
86
  This might be useful for multi-threading, map-reduce, or carrying on from where you left off if you're exploring a large search space.
87
87
 
88
+ You can specify an offset that affects all frequencies:
89
+
90
+ ```ruby
91
+ FrequencyEnumerator.new(distribution, :offset => 3).first
92
+ #=> { 'a' => 3, 'b' => 3 }
93
+ ```
94
+
88
95
  ## Real-world example
89
96
 
90
97
  My motivation for building this gem is to more intelligently brute-force the problem of finding [self-enumerating pangrams](http://en.wikipedia.org/wiki/Pangram#Self-enumerating_pangrams) by using classical literature to build a frequency distribution of English text.
@@ -8,6 +8,7 @@ class FrequencyEnumerator < Enumerable::Enumerator
8
8
  @bit_count = params[:bit_count] || 6
9
9
  @from = params[:from] || 0
10
10
  @to = params[:to] || limit
11
+ @offset = params[:offset] || 0
11
12
 
12
13
  raise_if_either_boundary_is_out_of_range
13
14
 
@@ -20,7 +21,8 @@ class FrequencyEnumerator < Enumerable::Enumerator
20
21
  (from..to).each do |i|
21
22
  binary = decomposer.decompose(i)
22
23
  bitmap = fragmented_bitmap(binary)
23
- yield composition(bitmap)
24
+ result = composition(bitmap)
25
+ yield offset(result)
24
26
  end
25
27
 
26
28
  self
@@ -50,6 +52,12 @@ class FrequencyEnumerator < Enumerable::Enumerator
50
52
  end
51
53
  end
52
54
 
55
+ def offset(result)
56
+ result.inject({}) do |h, (k, v)|
57
+ h.merge(k => (v + @offset))
58
+ end
59
+ end
60
+
53
61
  def sorted_keys
54
62
  return @sorted_keys if @sorted_keys
55
63
  sorter = @sorter.new(:bit_count => @bit_count)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frequency_enumerator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christopher Patuzzo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-05 00:00:00 Z
18
+ date: 2012-10-20 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec