-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.py
More file actions
29 lines (22 loc) · 907 Bytes
/
fraction.py
File metadata and controls
29 lines (22 loc) · 907 Bytes
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
"""
This file contains definition of a fraction class.
You should put complete class here. It must be named `Fraction` and must have the following properties:
- four basic mathematical operators defined;
- elegant conversion to string in the form '3/2';
- simplification and clean-up on construction: both attribute divided by the greatest common divisor
sign in the numerator, denumerator not zero (ValueError should be raised in such case), both attributes
must be integers (TypeError if not),
- method `decimal` returning the decimal value of the fraction.
"""
from math import gcd
class Fraction:
"""
Fraction class.
"""
def __init__(self, numer, denom):
self.numer = numer
self.denom = denom
def __str__(self):
return f"{self.numer}/{self.denom}"
def __eq__(self, other):
return self.numer == other.numer and self.denom == other.denom