-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_code.py
More file actions
61 lines (41 loc) · 1.42 KB
/
sample_code.py
File metadata and controls
61 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
from decimal import Decimal
from fractions import Fraction
from math import sqrt
class VectorBound:
def __init__(self, *coordinates):
self.coordinates = coordinates
def __abs__(self):
origin = [0] * len(self.coordinates)
return math.dist(origin, self.coordinates)
class VectorFree:
def __init__(self, *coordinates):
self.coordinates = coordinates
def __abs__(self):
return math.hypot(*self.coordinates)
def absolute_value_piecewise(x):
if x >= 0:
return x
else:
return -x
def absolute_value_piecewise_conditional_expression(x):
return x if x >= 0 else -x
def absolute_value_algebraic(x):
return sqrt(pow(x, 2))
def absolute_value_algebraic_exponents(x):
return (x**2) ** 0.5
def absolute_value_silly(x):
return float(str(x).replace("-", ""))
if __name__ == "__main__":
print(f"{absolute_value_piecewise(-12) = }")
print(f"{absolute_value_piecewise_conditional_expression(-12) = }")
print(f"{absolute_value_algebraic(-12) = }")
print(f"{absolute_value_algebraic_exponents(-12) = }")
print(f"{absolute_value_silly(-12) = }")
print(f"{abs(-12) = }")
print(f"{abs(-12.0) = }")
print(f"{abs(complex(3, 2)) = }")
print(f"{abs(Fraction('-3/4')) = }")
print(f"{abs(Decimal('-0.75')) = }")
print(f"{abs(VectorBound(0.42, 1.5, 0.87)) = }")
print(f"{abs(VectorFree(0.42, 1.5, 0.87)) = }")