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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79a123c813e3841e14a5e3c69cd9cabad1a235e8a31ad072d0413b633714a6cf
4
- data.tar.gz: 8517d24ed3f7cb6a0f003fe217532b2cf61e01fbc1b6c41b166d475e1cd6f227
3
+ metadata.gz: 8c25829111b4df390b8c30377bd879709f9e9c680498bc6687feaad4f30c21e4
4
+ data.tar.gz: 5d6a2c7cd763d2bb4be4c55bbc7c5d5925a108b68dbf16ae1a9ee5b6769fa16b
5
5
  SHA512:
6
- metadata.gz: e055b1d48cbe320e0aac31a57a31d270fea8b5b92a7c81e3ec15a1a1b3e26654070262dc4e3221819914c3efe245ba9ab0552aaefdb1f0951977c1a4635d7379
7
- data.tar.gz: 320b9a7bd0a458983d58e1b74023842524b82c5fd9c35232049c1b17dd99af6b7ee1f3986c3907db4f346e1ce9f776da0752735134201f1dbd106a6d82ad43c6
6
+ metadata.gz: fbbcd08a9bdfeffdd2779710d85aaa18497a72d3bcd8daf54e1a21cdd5978f2a12bde3d4144d263ad571ddaab1ea7e5ce256a536edd7ee6b6981a28b1781596c
7
+ data.tar.gz: f82c14b44bb1fbf8954978ec2a39769cb3586c40b24487fecb5c7a43effb5af838688fe0f95e866c1aaf4e29598648add8a71da3c33a42941d3c5e1605a3ab79
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /tmp/
9
9
  .rspec_status
10
10
  .DS_Store
11
+ Gemfile.lock
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weighted_list (0.1.0)
4
+ weighted_list (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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(hash)
10
- @hash = 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class WeightedList
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -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 = 'Choose random elements from a weighted list with or without replacement.'
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.1.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: Choose random elements from a weighted list with or without replacement.
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