weighted_list 0.1.0 → 0.2.0
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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/weighted_list.rb +3 -2
- data/lib/weighted_list/normalizer.rb +25 -0
- data/lib/weighted_list/version.rb +1 -1
- data/weighted_list.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c25829111b4df390b8c30377bd879709f9e9c680498bc6687feaad4f30c21e4
|
4
|
+
data.tar.gz: 5d6a2c7cd763d2bb4be4c55bbc7c5d5925a108b68dbf16ae1a9ee5b6769fa16b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbbcd08a9bdfeffdd2779710d85aaa18497a72d3bcd8daf54e1a21cdd5978f2a12bde3d4144d263ad571ddaab1ea7e5ce256a536edd7ee6b6981a28b1781596c
|
7
|
+
data.tar.gz: f82c14b44bb1fbf8954978ec2a39769cb3586c40b24487fecb5c7a43effb5af838688fe0f95e866c1aaf4e29598648add8a71da3c33a42941d3c5e1605a3ab79
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/weighted_list.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'weighted_list/normalizer'
|
3
4
|
require 'weighted_list/version'
|
4
5
|
require 'weighted_list/sampler'
|
5
6
|
|
6
7
|
class WeightedList
|
7
8
|
include Enumerable
|
8
9
|
|
9
|
-
def initialize(
|
10
|
-
@hash =
|
10
|
+
def initialize(collection)
|
11
|
+
@hash = Normalizer.call(collection)
|
11
12
|
end
|
12
13
|
|
13
14
|
def each(&block)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class WeightedList
|
2
|
+
class Normalizer
|
3
|
+
def self.call(collection)
|
4
|
+
new(collection).call
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(collection)
|
8
|
+
@hash = collection.to_h
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
hash.each_with_object({}) do |(item, weight), normalized_hash|
|
13
|
+
normalized_hash[item] = weight.fdiv(total_weight)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :hash
|
20
|
+
|
21
|
+
def total_weight
|
22
|
+
@total_weight ||= hash.values.reduce(&:+)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/weighted_list.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.email = ['jayson.virissimo@asu.edu']
|
13
13
|
|
14
14
|
spec.summary = 'Sample from a weighted list.'
|
15
|
-
spec.description = '
|
15
|
+
spec.description = 'Sample from an (enumerable) weighted list without replacement.'
|
16
16
|
spec.homepage = 'https://github.com/jaysonvirissimo/weighted_list'
|
17
17
|
spec.license = 'MIT'
|
18
18
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weighted_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jayson Virissimo
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
55
|
+
description: Sample from an (enumerable) weighted list without replacement.
|
56
56
|
email:
|
57
57
|
- jayson.virissimo@asu.edu
|
58
58
|
executables: []
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/weighted_list.rb
|
73
|
+
- lib/weighted_list/normalizer.rb
|
73
74
|
- lib/weighted_list/sampler.rb
|
74
75
|
- lib/weighted_list/version.rb
|
75
76
|
- weighted_list.gemspec
|