enumerable_weighted_sample 0.1.0 → 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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8263563d2736b3bd9e28aaac24a0ed1845d28975
|
4
|
+
data.tar.gz: f1c9c20a2c518b44cec323f0df2f06507dbdcae0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d7cf27c2782b9c6d63266cdf45f57b91742bbd345c47314ce2c31f5d76ae70f09f7b9e9b126f497a1b0a374fd2ccdcf44f92e3d88fb3db043b5d3f35041a826
|
7
|
+
data.tar.gz: 4ec1e831bbe9cea41d9f588b50b2949b54438abb22a65895b98cfea9c7b47ca0684b6af87eedd05e74ee67fb243303f84be8bc25e5120633fe3bd54677b1b09a
|
Binary file
|
@@ -49,69 +49,6 @@ module EnumerableWeightedSample
|
|
49
49
|
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
if $0 == __FILE__
|
55
|
-
|
56
|
-
class Array
|
57
|
-
def summarize
|
58
|
-
each_with_object(Hash.new(0)){|foo,h| h[foo] += 1 }.pretty
|
59
|
-
end
|
60
|
-
def pretty
|
61
|
-
map(&:inspect).join(", ")
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
class Hash
|
66
|
-
def pretty
|
67
|
-
sort_by{|_k,v|v}.map{|k,v| "#{k}: #{v}" }.join(", ")
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Foo
|
72
|
-
attr_accessor :name, :weight
|
73
|
-
def initialize(name, weight)
|
74
|
-
self.name = name
|
75
|
-
self.weight = weight
|
76
|
-
end
|
77
|
-
def to_s
|
78
|
-
name
|
79
|
-
end
|
80
|
-
def inspect
|
81
|
-
"#{name}: #{weight}"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
SAMPLES = 1_000
|
86
|
-
|
87
|
-
things = [
|
88
|
-
Foo.new("foo", 1),
|
89
|
-
Foo.new("bar", 2),
|
90
|
-
Foo.new("baz", 199),
|
91
|
-
Foo.new("bat", 198)
|
92
|
-
]
|
93
|
-
|
94
|
-
# puts things.inverse_weighted_sample(100).summarize
|
95
|
-
|
96
|
-
puts "input weights:"
|
97
|
-
puts things.pretty
|
98
|
-
puts
|
99
|
-
|
100
|
-
puts "#{SAMPLES}x weighted samples:"
|
101
|
-
weighted_things = things.weighted_sample(SAMPLES)
|
102
|
-
puts weighted_things.map(&:name).summarize
|
103
|
-
puts
|
104
|
-
|
105
|
-
puts "#{SAMPLES}x inverse weighted samples:"
|
106
|
-
inverse_weighted_things = things.inverse_weighted_sample(SAMPLES)
|
107
|
-
puts inverse_weighted_things.map(&:name).summarize
|
108
|
-
puts
|
109
|
-
|
110
|
-
puts "#{SAMPLES}x iterative rebalance:"
|
111
|
-
SAMPLES.times do
|
112
|
-
things.weighted_sample.weight -= 1
|
113
|
-
things.inverse_weighted_sample.weight += 1
|
114
|
-
end
|
115
|
-
puts things.pretty
|
116
|
-
|
52
|
+
class Array
|
53
|
+
include EnumerableWeightedSample
|
117
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerable_weighted_sample
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christine Oen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
|
+
- enumerable_weighted_sample-0.1.0.gem
|
71
72
|
- enumerable_weighted_sample.gemspec
|
72
73
|
- lib/enumerable_weighted_sample.rb
|
73
74
|
- lib/enumerable_weighted_sample/version.rb
|