Skip to content

Commit af182d8

Browse files
committed
Python abs() - Review updates
1 parent 89db4a2 commit af182d8

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

python-absolute-value/sample_code.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from math import sqrt
55

66

7-
class Vector1:
7+
class VectorBound:
88
def __init__(self, *coordinates):
99
self.coordinates = coordinates
1010

@@ -13,49 +13,49 @@ def __abs__(self):
1313
return math.dist(origin, self.coordinates)
1414

1515

16-
class Vector2:
16+
class VectorFree:
1717
def __init__(self, *coordinates):
1818
self.coordinates = coordinates
1919

2020
def __abs__(self):
2121
return math.hypot(*self.coordinates)
2222

2323

24-
def absolute_value1(x):
24+
def absolute_value_piecewise(x):
2525
if x >= 0:
2626
return x
2727
else:
2828
return -x
2929

3030

31-
def absolute_value2(x):
31+
def absolute_value_piecewise_conditional_expression(x):
3232
return x if x >= 0 else -x
3333

3434

35-
def absolute_value3(x):
35+
def absolute_value_algebraic(x):
3636
return sqrt(pow(x, 2))
3737

3838

39-
def absolute_value4(x):
39+
def absolute_value_algebraic_exponents(x):
4040
return (x**2) ** 0.5
4141

4242

43-
def absolute_value5(x):
43+
def absolute_value_silly(x):
4444
return float(str(x).replace("-", ""))
4545

4646

4747
if __name__ == "__main__":
48-
print(f"{absolute_value1(-12) = }")
49-
print(f"{absolute_value2(-12) = }")
50-
print(f"{absolute_value3(-12) = }")
51-
print(f"{absolute_value4(-12) = }")
52-
print(f"{absolute_value5(-12) = }")
48+
print(f"{absolute_value_piecewise(-12) = }")
49+
print(f"{absolute_value_piecewise_conditional_expression(-12) = }")
50+
print(f"{absolute_value_algebraic(-12) = }")
51+
print(f"{absolute_value_algebraic_exponents(-12) = }")
52+
print(f"{absolute_value_silly(-12) = }")
5353

5454
print(f"{abs(-12) = }")
5555
print(f"{abs(-12.0) = }")
5656
print(f"{abs(complex(3, 2)) = }")
5757
print(f"{abs(Fraction('-3/4')) = }")
5858
print(f"{abs(Decimal('-0.75')) = }")
5959

60-
print(f"{abs(Vector1(0.42, 1.5, 0.87)) = }")
61-
print(f"{abs(Vector2(0.42, 1.5, 0.87)) = }")
60+
print(f"{abs(VectorBound(0.42, 1.5, 0.87)) = }")
61+
print(f"{abs(VectorFree(0.42, 1.5, 0.87)) = }")

0 commit comments

Comments
 (0)