From 0c342dc37fa684e0594607b61c1297bbfd0073c8 Mon Sep 17 00:00:00 2001 From: Vhizux Date: Fri, 23 Oct 2020 21:38:54 +0200 Subject: [PATCH 1/8] added binomial distribution function --- .vscode/settings.json | 5 +++ mathgenerator/funcs/__init__.py | 1 + mathgenerator/funcs/binomial_distribution.py | 42 ++++++++++++++++++++ test.py | 4 +- 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 mathgenerator/funcs/binomial_distribution.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9fadd3a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.linting.pylintEnabled": false, + "python.linting.flake8Enabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/mathgenerator/funcs/__init__.py b/mathgenerator/funcs/__init__.py index 575dcd2..8bb773a 100644 --- a/mathgenerator/funcs/__init__.py +++ b/mathgenerator/funcs/__init__.py @@ -111,3 +111,4 @@ from .decimal_to_bcd import * from .circumference import * from .combine_like_terms import * from .conditional_probability import * +from .binomial_distribution import * diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py new file mode 100644 index 0000000..73ccbe6 --- /dev/null +++ b/mathgenerator/funcs/binomial_distribution.py @@ -0,0 +1,42 @@ +from .__init__ import * + + +def factorial(n): + + if n == 1 or n == 0: + return 1 + else: + return n*factorial(n-1) + + +def newton_symbol(n, k): + return factorial(n)/(factorial(k)*factorial(n-k)) + + +def binomialDistFunc(): + + rejected_fraction = float(random.randint(30, 40)) + random.random() + batch = random.randint(10, 20) + rejections = random.randint(1, 9) + + answer = 0 + + rejected_fraction = round(rejected_fraction, 2) + + problem = "A manufacturer of metal pistons finds that, on average, {0:}% "\ + "of the pistons they manufacture are rejected because " \ + "they are incorrectly sized. What is the probability that a "\ + "batch of {1:} pistons will contain no more than {2:} " \ + "rejected pistons?".format(rejected_fraction, batch, rejections) + + for i in range(0, rejections+1): + answer += newton_symbol(batch, i)*((rejected_fraction/100.)**i) * \ + ((1 - (rejected_fraction/100.))**(batch-i)) + + answer = round(100*answer, 2) + + return problem, answer + + +binomial_distribution = Generator("Binomial distribution", 107, + "P(X Date: Sun, 25 Oct 2020 14:31:37 +0100 Subject: [PATCH 2/8] small improvements --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 9fadd3a..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "python.linting.pylintEnabled": false, - "python.linting.flake8Enabled": true, - "python.linting.enabled": true -} \ No newline at end of file From e5efd5f92d88377c12968547f7144fabaf358de4 Mon Sep 17 00:00:00 2001 From: Vhizux Date: Sun, 25 Oct 2020 21:54:58 +0100 Subject: [PATCH 3/8] converted ints to floats --- mathgenerator/funcs/binomial_distribution.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py index 73ccbe6..31e8a21 100644 --- a/mathgenerator/funcs/binomial_distribution.py +++ b/mathgenerator/funcs/binomial_distribution.py @@ -30,8 +30,8 @@ def binomialDistFunc(): "rejected pistons?".format(rejected_fraction, batch, rejections) for i in range(0, rejections+1): - answer += newton_symbol(batch, i)*((rejected_fraction/100.)**i) * \ - ((1 - (rejected_fraction/100.))**(batch-i)) + answer += newton_symbol(float(batch), float(i))*((rejected_fraction/100.)**float(i)) * \ + ((1 - (rejected_fraction/100.))**(float(batch)-float(i))) answer = round(100*answer, 2) From 36fbf848393e2a2ce8cca78685c662a91745beb9 Mon Sep 17 00:00:00 2001 From: Vhizux Date: Sun, 25 Oct 2020 21:58:31 +0100 Subject: [PATCH 4/8] lint fix --- mathgenerator/funcs/binomial_distribution.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py index 31e8a21..b47830a 100644 --- a/mathgenerator/funcs/binomial_distribution.py +++ b/mathgenerator/funcs/binomial_distribution.py @@ -29,11 +29,11 @@ def binomialDistFunc(): "batch of {1:} pistons will contain no more than {2:} " \ "rejected pistons?".format(rejected_fraction, batch, rejections) - for i in range(0, rejections+1): - answer += newton_symbol(float(batch), float(i))*((rejected_fraction/100.)**float(i)) * \ - ((1 - (rejected_fraction/100.))**(float(batch)-float(i))) + for i in range(0, rejections + 1): + answer += newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \ + ((1 - (rejected_fraction/100.)) ** (float(batch)-float(i))) - answer = round(100*answer, 2) + answer = round(100 * answer, 2) return problem, answer From 226f6bb36dcd751874ff8aa356672a57e1744daa Mon Sep 17 00:00:00 2001 From: Vhizux Date: Sun, 25 Oct 2020 21:59:08 +0100 Subject: [PATCH 5/8] lint fix --- mathgenerator/funcs/binomial_distribution.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py index b47830a..8fc5f95 100644 --- a/mathgenerator/funcs/binomial_distribution.py +++ b/mathgenerator/funcs/binomial_distribution.py @@ -6,11 +6,11 @@ def factorial(n): if n == 1 or n == 0: return 1 else: - return n*factorial(n-1) + return n * factorial(n-1) def newton_symbol(n, k): - return factorial(n)/(factorial(k)*factorial(n-k)) + return factorial(n) / (factorial(k) * factorial(n-k)) def binomialDistFunc(): From 6079b20c2988f208af27c18886d76ac82017e15b Mon Sep 17 00:00:00 2001 From: Vhizux Date: Sun, 25 Oct 2020 22:03:12 +0100 Subject: [PATCH 6/8] lint fix --- .vscode/settings.json | 5 +++++ mathgenerator/funcs/binomial_distribution.py | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9fadd3a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.linting.pylintEnabled": false, + "python.linting.flake8Enabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py index 8fc5f95..5a4e404 100644 --- a/mathgenerator/funcs/binomial_distribution.py +++ b/mathgenerator/funcs/binomial_distribution.py @@ -6,11 +6,11 @@ def factorial(n): if n == 1 or n == 0: return 1 else: - return n * factorial(n-1) + return n * factorial(n - 1) def newton_symbol(n, k): - return factorial(n) / (factorial(k) * factorial(n-k)) + return factorial(n) / (factorial(k) * factorial(n - k)) def binomialDistFunc(): @@ -31,12 +31,12 @@ def binomialDistFunc(): for i in range(0, rejections + 1): answer += newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \ - ((1 - (rejected_fraction/100.)) ** (float(batch)-float(i))) + ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i))) answer = round(100 * answer, 2) return problem, answer -binomial_distribution = Generator("Binomial distribution", 107, +binomial_distribution = Generator("Binomial distribution", 107, "P(X Date: Sun, 25 Oct 2020 22:03:24 +0100 Subject: [PATCH 7/8] lint fix --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 9fadd3a..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "python.linting.pylintEnabled": false, - "python.linting.flake8Enabled": true, - "python.linting.enabled": true -} \ No newline at end of file From 1ed0897310b396a67ee2abc4d0416a3b675bec39 Mon Sep 17 00:00:00 2001 From: Vhizux Date: Sun, 25 Oct 2020 22:06:40 +0100 Subject: [PATCH 8/8] lint fix --- mathgenerator/funcs/binomial_distribution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathgenerator/funcs/binomial_distribution.py b/mathgenerator/funcs/binomial_distribution.py index 5a4e404..03166e3 100644 --- a/mathgenerator/funcs/binomial_distribution.py +++ b/mathgenerator/funcs/binomial_distribution.py @@ -31,7 +31,7 @@ def binomialDistFunc(): for i in range(0, rejections + 1): answer += newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \ - ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i))) + ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i))) answer = round(100 * answer, 2)