Implement a Rational number class in Python. Your class should utilize Python's magic methods to allow operations on Rational numbers with normal mathematical operators. Additionally, your Rational number should be fully simplified upon initialization:
x = Rational(5, 25)
print x # 1/5
I expect the following operations to work, both reversed and normal:
Additionally, your class should have a working string representation available (e.g., __str__()
).
You may want to have the following import:
from fractions import gcd
Now you have a greatest common divisor function. Huzzah!