-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction_class.py
More file actions
64 lines (54 loc) · 2.49 KB
/
Copy pathfraction_class.py
File metadata and controls
64 lines (54 loc) · 2.49 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
class Fraction:
def __init__(self, numerator, denominator):
if denominator == 0:
raise ValueError("Denominator cannot be zero")
self.numerator = numerator
self.denominator = denominator
self._simplify() # Simplify the fraction upon initialization
def _find_gcd(self, a, b):
a, b = abs(a), abs(b) # Work with absolute values
while b != 0:
a, b = b, a % b
return a
def _simplify(self):
gcd = self._find_gcd(self.numerator, self.denominator)
self.numerator //= gcd
self.denominator //= gcd
if self.denominator < 0:
self.numerator = -self.numerator
self.denominator = -self.denominator
def add(self, other):
# Add two fractions: a/b + c/d = (a*d + c*b) / (b*d)
new_numerator = self.numerator * other.denominator + other.numerator * self.denominator
new_denominator = self.denominator * other.denominator
return Fraction(new_numerator, new_denominator)
def subtract(self, other):
# Subtract two fractions: a/b - c/d = (a*d - c*b) / (b*d)
new_numerator = self.numerator * other.denominator - other.numerator * self.denominator
new_denominator = self.denominator * other.denominator
return Fraction(new_numerator, new_denominator)
def multiply(self, other):
# Multiply two fractions: (a/b) * (c/d) = (a*c) / (b*d)
new_numerator = self.numerator * other.numerator
new_denominator = self.denominator * other.denominator
return Fraction(new_numerator, new_denominator)
def divide(self, other):
if other.numerator == 0:
raise ValueError("Cannot divide by zero fraction")
new_numerator = self.numerator * other.denominator
new_denominator = self.denominator * other.numerator
return Fraction(new_numerator, new_denominator)
def __eq__(self, other):
# Compare two fractions for equality
if not isinstance(other, Fraction):
return False
return (self.numerator == other.numerator and
self.denominator == other.denominator)
def __str__(self):
# String representation of the fraction
if self.denominator == 1:
return str(self.numerator)
return f"{self.numerator}/{self.denominator}"
def __repr__(self):
# Detailed representation for debugging
return f"Fraction({self.numerator}, {self.denominator})"