-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractions.py
More file actions
87 lines (69 loc) · 3.09 KB
/
Copy pathfractions.py
File metadata and controls
87 lines (69 loc) · 3.09 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@namespace("fractions")
from Promethium import ValueError
# A small, opt-in subset of Python's fractions module: just `Fraction`
# itself, always stored in lowest terms with a positive denominator (like
# CPython's), reusing `math.gcd` (fully qualified — bare calls to a
# concrete function from another namespace aren't reliably resolved via
# ambient `DefaultUses`, the same finding `heapq.py`/`statistics.py`
# document).
class Fraction:
numerator: int
denominator: int
def __init__(self, numerator: int, denominator: int):
if denominator == 0:
raise ValueError("Fraction(n, 0)")
sign: int = 1
if denominator < 0:
sign = -1
n: int = numerator * sign
d: int = denominator * sign
divisor: int = math.gcd(n, d)
if divisor == 0:
divisor = 1
self.numerator = n / divisor
self.denominator = d / divisor
def __init__(self, numerator: int):
self.numerator = numerator
self.denominator = 1
def __add__(self, other: Fraction) -> Fraction:
return Fraction(self.numerator * other.denominator + other.numerator * self.denominator, self.denominator * other.denominator)
def __sub__(self, other: Fraction) -> Fraction:
return Fraction(self.numerator * other.denominator - other.numerator * self.denominator, self.denominator * other.denominator)
def __mul__(self, other: Fraction) -> Fraction:
return Fraction(self.numerator * other.numerator, self.denominator * other.denominator)
def __truediv__(self, other: Fraction) -> Fraction:
return Fraction(self.numerator * other.denominator, self.denominator * other.numerator)
def __eq__(self, other: Fraction) -> bool:
return self.numerator == other.numerator and self.denominator == other.denominator
def __lt__(self, other: Fraction) -> bool:
return self.numerator * other.denominator < other.numerator * self.denominator
def __le__(self, other: Fraction) -> bool:
return self.numerator * other.denominator <= other.numerator * self.denominator
def __neg__(self) -> Fraction:
return Fraction(-self.numerator, self.denominator)
def __abs__(self) -> Fraction:
if self.numerator < 0:
return Fraction(-self.numerator, self.denominator)
return Fraction(self.numerator, self.denominator)
def __pow__(self, exponent: int) -> Fraction:
exp: int = exponent
negative: bool = False
if exp < 0:
negative = True
exp = -exp
resultNum: int = 1
resultDen: int = 1
index: int = 0
while index < exp:
resultNum *= self.numerator
resultDen *= self.denominator
index += 1
if negative:
return Fraction(resultDen, resultNum)
return Fraction(resultNum, resultDen)
def __float__(self) -> float:
return (self.numerator * 1.0) / self.denominator
def __str__(self) -> str:
if self.denominator == 1:
return "" + self.numerator
return "" + self.numerator + "/" + self.denominator