From a075368f96c5bc4c106447fb288ac0ec5b8da7b7 Mon Sep 17 00:00:00 2001 From: gabriellypinto Date: Sat, 7 Oct 2023 11:43:12 -0300 Subject: [PATCH 1/3] fix input, add some error tests and add note about random --- maths/monte_carlo.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 474f1f65deb4..1b4b09877eaf 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -17,7 +17,32 @@ def pi_estimator(iterations: int): 4. After all the dots are placed, divide the dots in the circle by the total. 5. Multiply this value by 4 to get your estimate of pi. 6. Print the estimated and numpy value of pi + + Note: + this function relies on a random method. So consistent testing is + impossible! + + >>> pi_estimator("a") + Traceback (most recent call last): + ... + ValueError: iterations must be a integer! + + >>> pi_estimator(-1) + Traceback (most recent call last): + ... + ValueError: iterations must be at least 1! + + >>> pi_estimator(0) + Traceback (most recent call last): + ... + ValueError: iterations must be at least 1! """ + try: + iterations = int(iterations) + except Exception as e: + raise ValueError("iterations must be a integer!") from e + if iterations < 1: + raise ValueError("iterations must be at least 1!") # A local function to see if a dot lands in the circle. def is_in_circle(x: float, y: float) -> bool: @@ -33,9 +58,8 @@ def is_in_circle(x: float, y: float) -> bool: ) # The ratio of the area for circle to square is pi/4. pi_estimate = proportion * 4 - print(f"The estimated value of pi is {pi_estimate}") - print(f"The numpy value of pi is {pi}") - print(f"The total error is {abs(pi - pi_estimate)}") + error_value = abs(pi - pi_estimate) + return pi_estimate, error_value def area_under_curve_estimator( @@ -60,6 +84,11 @@ def area_under_curve_estimator( c. Expected value = average of the function evaluations 4. Estimated value of integral = Expected value * (max_value - min_value) 5. Returns estimated value + + Note: + this function relies on a random method. So consistent testing is + impossible! + """ return mean( @@ -76,6 +105,10 @@ def area_under_line_estimator_check( 1. Calls "area_under_curve_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value + + Note: + this function relies on a random method. So consistent testing is + impossible! """ def identity_function(x: float) -> float: @@ -102,6 +135,10 @@ def identity_function(x: float) -> float: def pi_estimator_using_area_under_curve(iterations: int) -> None: """ Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi + + Note: + this function relies on a random method. So consistent testing is + impossible! """ def function_to_integrate(x: float) -> float: From 9169e8aaa89ba1d14c598d298d6aa78c2bf4afb7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 14:44:31 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/monte_carlo.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 1b4b09877eaf..221d5b5ed453 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -17,21 +17,21 @@ def pi_estimator(iterations: int): 4. After all the dots are placed, divide the dots in the circle by the total. 5. Multiply this value by 4 to get your estimate of pi. 6. Print the estimated and numpy value of pi - + Note: this function relies on a random method. So consistent testing is impossible! - + >>> pi_estimator("a") Traceback (most recent call last): ... ValueError: iterations must be a integer! - + >>> pi_estimator(-1) Traceback (most recent call last): ... ValueError: iterations must be at least 1! - + >>> pi_estimator(0) Traceback (most recent call last): ... @@ -84,11 +84,11 @@ def area_under_curve_estimator( c. Expected value = average of the function evaluations 4. Estimated value of integral = Expected value * (max_value - min_value) 5. Returns estimated value - + Note: this function relies on a random method. So consistent testing is impossible! - + """ return mean( @@ -105,7 +105,7 @@ def area_under_line_estimator_check( 1. Calls "area_under_curve_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value - + Note: this function relies on a random method. So consistent testing is impossible! @@ -135,7 +135,7 @@ def identity_function(x: float) -> float: def pi_estimator_using_area_under_curve(iterations: int) -> None: """ Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi - + Note: this function relies on a random method. So consistent testing is impossible! From 42296ab1853830d7f77e73cf239c61cee9d5fbdd Mon Sep 17 00:00:00 2001 From: gabriellypinto Date: Sat, 7 Oct 2023 11:46:07 -0300 Subject: [PATCH 3/3] fix catch --- maths/monte_carlo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 221d5b5ed453..ecbd09439faf 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -39,7 +39,7 @@ def pi_estimator(iterations: int): """ try: iterations = int(iterations) - except Exception as e: + except ValueError as e: raise ValueError("iterations must be a integer!") from e if iterations < 1: raise ValueError("iterations must be at least 1!")