Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions week2/4-Fractions/p04_anImmutableFraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Fraction:

def __init__(self, nominator, denominator):
self.nomi = nominator
self.denomi = denominator

def __str__(self):
if self.nomi % self.denomi == 0:
return str(int(self.nomi / self.denomi))
else:
return "{} / {}".format(self.nomi, self.denomi)

def __repr__(self):
return self.__str__()

def __eq__(self, other):
if self.nomi / self.denomi == other.nomi / other.denomi:
return True
return False

@staticmethod
def reduce_fraction(fraction):
for i in range(fraction.nomi, 0, -1):
if fraction.nomi % i == 0 and fraction.denomi % i == 0:
fraction.nomi /= i
fraction.denomi /= i
return Fraction(int(fraction.nomi), int(fraction.denomi))

def __add__(self, other):
if isinstance(self, int):
self = Fraction(self, 1)

if isinstance(other, int):
other = Fraction(other, 1)

new_nominator = self.nomi * other.denomi + self.denomi * other.nomi
new_denominator = self.denomi * other.denomi
new_fraction = Fraction(new_nominator, new_denominator)
return Fraction.reduce_fraction(new_fraction)

def __sub__(self, other):
if isinstance(self, int):
self = Fraction(self, 1)

if isinstance(other, int):
other = Fraction(other, 1)

new_nominator = self.nomi * other.denomi - self.denomi * other.nomi
new_denominator = self.denomi * other.denomi
new_fraction = Fraction(new_nominator, new_denominator)
return Fraction.reduce_fraction(new_fraction)

def __mul__(self, other):
if isinstance(self, int):
self = Fraction(self, 1)

if isinstance(other, int):
other = Fraction(other, 1)

new_nominator = self.nomi * other.nomi
new_denominator = self.denomi * other.denomi
new_fraction = Fraction(new_nominator, new_denominator)
return Fraction.reduce_fraction(new_fraction)
78 changes: 78 additions & 0 deletions week2/4-Fractions/p04_test_anImmutableFraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import unittest

from p04_anImmutableFraction import Fraction


class TestFraction(unittest.TestCase):

def setUp(self):
self.a = Fraction(1, 2)
self.b = Fraction(2, 4)
self.c = Fraction(1, 2)
self.d = Fraction(5, 6)
self.e = Fraction(2, 2)
self.f = Fraction(7, 13)
self.g = Fraction(4, 18)

def test_constructor(self):
self.assertIsInstance(self.a, Fraction)

def test_to_str1(self):
self.assertEqual(str(self.a), "1 / 2")

def test_to_str2(self):
self.assertEqual(str(self.e), '1')

def test_eq(self):
self.assertTrue(self.a == self.c)
self.assertTrue(self.a == self.b)

def test_eq_false(self):
self.assertFalse(self.a == self.d)

def test_reduce_fraction(self):
self.assertEqual(Fraction.reduce_fraction(self.b), Fraction(1, 2))

# 1 / 2 + 2 / 4 =
def test_add1(self):
self.assertEqual(self.a + self.b, Fraction(1, 1))

# 1 / 2 + 5 / 6 =
def test_add2(self):
self.assertEqual(self.a + self.d, Fraction(4, 3))

# 1 / 2 + 0 =
def test_add3(self):
self.assertEqual(self.a + 0, Fraction(1, 2))

# 1 / 2 + 2 =
def test_add4(self):
self.assertEqual(self.a + 2, Fraction(5, 2))

# 1 / 2 - 1 / 2 =
def test_sub1(self):
self.assertTrue(self.a - self.b, Fraction(6, 6))

# 1 / 2 - 5 / 6 =
def test_sub2(self):
self.assertEqual(self.a - self.d, Fraction(-1, 3))

# 1 / 2 * 1 / 2 =
def test_multiplication1(self):
self.assertEqual((self.a * self.b), Fraction(1, 4))

# 1 / 2 * 5 / 6 =
def test_multiplication2(self):
self.assertEqual((self.a * self.d), Fraction(5, 12))

# 7 / 13 * 4 / 18 =
def test_multiplication3(self):
self.assertEqual(self.f * self.g, Fraction(14, 117))

# 1 / 2 * 5 =
def test_multiplication4(self):
self.assertEqual(self.a * 5, Fraction(5, 2))


if __name__ == "__main__":
unittest.main()