math_probability 0.1.1 → 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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 126b15b6b5d89b948f88d15ed649551fca41dd58
|
4
|
+
data.tar.gz: a6cbc805e89281180ccd401ed840c6652adf11be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8aaf432baa59badd5b8e441aafdbae9bf24e88ef1724c332d008601e9fdac24e993bacfdd904e52db183638fa2df437d7a5c5eb8415890f6a0eb05c84d848452
|
7
|
+
data.tar.gz: b5ec7a3931ec40893e0c0ddb64b519b4d2d34970132317a6b875cc9eeb10c05235e2fd39686c670e5dfbbfe4535b5ec596fc20cb00f33b361e43e2ab2f8f70a7
|
data/lib/math_probability.rb
CHANGED
@@ -27,10 +27,22 @@ module MathProbability
|
|
27
27
|
(((objects.factoral)/((objects-at_a_time).factoral))*(1.0/(at_a_time.factoral))).to_i
|
28
28
|
end
|
29
29
|
|
30
|
-
def self.probability(
|
30
|
+
def self.probability(choices, outcomes, reduce=true)
|
31
31
|
answer ||= []
|
32
|
-
a =
|
33
|
-
answer << a << "#{a*100}%" << "#{
|
32
|
+
a = choices.to_f / outcomes
|
33
|
+
answer << a << "#{a*100}%" << (reduce ? reduced_fraction(choices, outcomes) : "#{choices}/#{outcomes}")
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def self.reduced_fraction(nominator, denominator)
|
39
|
+
lcd ||= denominator
|
40
|
+
denominator.downto(2).each do |n|
|
41
|
+
if nominator % n == 0 && denominator % n == 0 && n != denominator && n < lcd
|
42
|
+
lcd = n
|
43
|
+
end
|
44
|
+
end
|
45
|
+
lcd != denominator ? "#{nominator/lcd}/#{denominator/lcd}" : "#{nominator}/#{denominator}"
|
34
46
|
end
|
35
47
|
|
36
48
|
end
|
@@ -31,6 +31,14 @@ describe MathProbability::Probability do
|
|
31
31
|
it "should return all forms of Probability" do
|
32
32
|
subject.probability(1,2).must_equal([0.5, "50.0%", "1/2"])
|
33
33
|
end
|
34
|
+
|
35
|
+
it "should return a reduced fraction" do
|
36
|
+
subject.probability(2,4).must_equal([0.5, "50.0%", "1/2"])
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not return a reduced fraction if flag is set to false" do
|
40
|
+
subject.probability(2,4,false).must_equal([0.5, "50.0%", "2/4"])
|
41
|
+
end
|
34
42
|
|
35
43
|
end
|
36
44
|
|