-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.py
More file actions
38 lines (31 loc) · 1.02 KB
/
Copy pathfraction.py
File metadata and controls
38 lines (31 loc) · 1.02 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
from mathops.gcd import computeGCD as gcd
class Fraction:
def __init__(self, numerator, denominator):
"""
How to initialize a fraction?
"""
if denominator == 0:
raise "Denominator cannot be zero"
self.numerator = numerator // gcd(numerator, denominator)
self.denominator = denominator // gcd(numerator, denominator)
def __str__(self):
"""
How to print (str) a fraction?
"""
return f"{self.numerator}/{self.denominator}"
def __eq__(self, other):
"""
When are two fractions equal?
"""
return (
self.numerator == other.numerator and self.denominator == other.denominator
)
def __add__(self, other):
"""
How to add two fractions
"""
numerator = (
self.numerator * other.denominator + self.denominator * other.numerator
)
denominator = self.denominator * other.denominator
return Fraction(numerator, denominator)