Skip to content

Commit 4f9d432

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
fixup! Format Python code with psf/black push
1 parent b36e46b commit 4f9d432

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

maths/monte_carlo.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def in_circle(x: float, y: float) -> bool:
2626

2727
# The proportion of guesses that landed in the circle
2828
proportion = mean(
29-
int(in_circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0))) for _ in range(iterations)
29+
int(in_circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0)))
30+
for _ in range(iterations)
3031
)
3132
# The ratio of the area for circle to square is pi/4.
3233
pi_estimate = proportion * 4
@@ -35,9 +36,9 @@ def in_circle(x: float, y: float) -> bool:
3536
print("The total error is ", abs(pi - pi_estimate))
3637

3738

38-
def area_under_line_estimator(iterations: int,
39-
min_value: float=0.0,
40-
max_value: float=1.0) -> float:
39+
def area_under_line_estimator(
40+
iterations: int, min_value: float = 0.0, max_value: float = 1.0
41+
) -> float:
4142
"""
4243
An implementation of the Monte Carlo method to find area under
4344
y = x where x lies between min_value to max_value
@@ -49,24 +50,28 @@ def area_under_line_estimator(iterations: int,
4950
4. Actual value = (max_value^2 - min_value^2) / 2
5051
5. Returns estimated value
5152
"""
52-
return mean(uniform(min_value, max_value) for _ in range(iterations)) * (max_value - min_value)
53+
return mean(uniform(min_value, max_value) for _ in range(iterations)) * (
54+
max_value - min_value
55+
)
5356

5457

55-
def area_under_line_estimator_check(iterations: int,
56-
min_value: float=0.0,
57-
max_value: float=1.0) -> None:
58+
def area_under_line_estimator_check(
59+
iterations: int, min_value: float = 0.0, max_value: float = 1.0
60+
) -> None:
5861
"""
5962
Checks estimation error for area_under_line_estimator func
6063
1. Calls "area_under_line_estimator" function
6164
2. Compares with the expected value
6265
3. Prints estimated, expected and error value
6366
"""
64-
67+
6568
estimated_value = area_under_line_estimator(iterations, min_value, max_value)
66-
expected_value = (max_value*max_value - min_value*min_value) / 2
67-
69+
expected_value = (max_value * max_value - min_value * min_value) / 2
70+
6871
print("******************")
69-
print("Estimating area under y=x where x varies from ",min_value, " to ",max_value)
72+
print(
73+
"Estimating area under y=x where x varies from ", min_value, " to ", max_value
74+
)
7075
print("Estimated value is ", estimated_value)
7176
print("Expected value is ", expected_value)
7277
print("Total error is ", abs(estimated_value - expected_value))

0 commit comments

Comments
 (0)