probability_to_friendly_string 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3593585eb866ea1da80396ec1d860a33e0c7910f
4
+ data.tar.gz: cc800c83ece8650d8845d2b3c4ea52761aca2164
5
+ SHA512:
6
+ metadata.gz: 3908e471317c8b18de71d56bc5219f9e018c1e237efb5002cb21d2b6589b422cca887331d4a1ee353d7f7502a8adeda76dc7871cc57f54c50fb1e7f939fc371d
7
+ data.tar.gz: fe35bf7fe7cb03bc58e361c7c515f00426c4849e2eae488e369514ef20f5f59d6d7ffffeff596fee19f94fb39a56bd65aadf4906ec6900a9d88a12fdc894d03b
@@ -0,0 +1,92 @@
1
+ module ProbabilityToFriendlyString
2
+ class FriendlyProbability
3
+ include Comparable
4
+ attr_reader :numerator, :denominator, :friendlyString
5
+
6
+ @@fractionsData = nil
7
+ def initialize(numerator, denominator, friendlyString = nil)
8
+ @numerator = numerator
9
+ @denominator = denominator
10
+ if friendlyString
11
+ @friendlyString = friendlyString
12
+ else
13
+ @friendlyString = "%d in %d" % [numerator, denominator]
14
+ end
15
+ end
16
+
17
+ def self._addFraction(fractionsData, numerator, denominator)
18
+ fractionsData << [numerator.to_f/denominator, numerator, denominator]
19
+ end
20
+ def self._createFractionsData
21
+ if @@fractionsData
22
+ return
23
+ end
24
+ fractionsData = []
25
+ (2..10).each do |d|
26
+ (1..d).each do |n|
27
+ if n.gcd(d) == 1
28
+ _addFraction(fractionsData, n, d)
29
+ end
30
+ end
31
+ end
32
+ [12, 15, 20, 30, 40, 50, 60, 80, 100].each do |d|
33
+ _addFraction(fractionsData, 1, d)
34
+ _addFraction(fractionsData, d - 1, d)
35
+ end
36
+ @@fractionsData = fractionsData.sort
37
+ end
38
+
39
+ def self.fromProbability(f)
40
+ if f < 0 or f > 1
41
+ raise RangeError, "f is less than 0 or greater than 1"
42
+ end
43
+ if f == 0
44
+ return FriendlyProbability.new 0, 1
45
+ elsif f == 1
46
+ return FriendlyProbability.new 1, 1
47
+ elsif f > 0.99
48
+ return FriendlyProbability.new 99, 100, ">99 in 100"
49
+ elsif f < 0.01
50
+ return FriendlyProbability.new 1, 100, "<1 in 100"
51
+ end
52
+
53
+ FriendlyProbability._createFractionsData
54
+ # index of the least element > f
55
+ right = @@fractionsData.bsearch_index {|x| x[0] > f}
56
+ if right
57
+ left = right - 1
58
+ else
59
+ left = @@fractionsData.length - 1
60
+ end
61
+ if (left == (@@fractionsData.length - 1) or (left >= 0 and f - @@fractionsData[left][0] < @@fractionsData[right][0] - f))
62
+ return FriendlyProbability.new @@fractionsData[left][1], @@fractionsData[left][2]
63
+ else
64
+ return FriendlyProbability.new @@fractionsData[right][1], @@fractionsData[right][2]
65
+ end
66
+ end
67
+
68
+ def to_s
69
+ return "#{@numerator}/#{@denominator} (text: \"#{@friendlyString}\")"
70
+ end
71
+
72
+ def <=>(another_friendly_string)
73
+ if self.numerator < another_friendly_string.numerator
74
+ -1
75
+ elsif self.numerator > another_friendly_string.numerator
76
+ 1
77
+ end
78
+ if self.denominator < another_friendly_string.denominator
79
+ -1
80
+ elsif self.denominator > another_friendly_string.denominator
81
+ 1
82
+ end
83
+ if self.friendlyString < another_friendly_string.friendlyString
84
+ -1
85
+ elsif self.friendlyString > another_friendly_string.friendlyString
86
+ 1
87
+ else
88
+ 0
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/probability_to_friendly_string'
3
+
4
+ class FriendlyProbabilityTests < Test::Unit::TestCase
5
+ def test_all_cases
6
+ directory = File.absolute_path(File.dirname(__FILE__))
7
+ path = File.join(directory, 'testCases.txt')
8
+ while !File.exists?(path)
9
+ directory = File.dirname(directory)
10
+ path = File.join(directory, 'testCases.txt')
11
+ end
12
+ lineNumber = 0
13
+ File.open(path).each do |fullLine|
14
+ lineNumber += 1
15
+ line = fullLine.strip
16
+ if (line.start_with?('#') or line.empty?)
17
+ next
18
+ end
19
+ parts = line.split(',')
20
+ case parts.length
21
+ when 3
22
+ expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i
23
+ when 4
24
+ expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i, parts[3]
25
+ else
26
+ flunk "Line badly formatted: #{line} (line #{lineNumber})"
27
+ end
28
+ actual = ProbabilityToFriendlyString::FriendlyProbability.fromProbability(parts[0].to_f)
29
+ assert_equal expected, actual, "Called on #{parts[0]} (line #{lineNumber})"
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: probability_to_friendly_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg Stoll
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Make probabilities more intuitive by converting them to odds (i.e. 72.3%
14
+ becomes "5 in 7")
15
+ email: greg@gregstoll.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/probability_to_friendly_string.rb
21
+ - test/friendly_probability_tests.rb
22
+ homepage: https://github.com/gregstoll/probabilityToFriendlyString
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.2.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Make probabilities more intuitive by converting them to odds
46
+ test_files:
47
+ - test/friendly_probability_tests.rb