From 7aaff2261c99eaa0ae731e1193a0c972b8c77a8a Mon Sep 17 00:00:00 2001 From: NikolaPavlov Date: Sun, 19 Apr 2015 21:30:57 +0300 Subject: [PATCH] add week2 Fraction --- week2/4-Fractions/p04_anImmutableFraction.py | 63 +++++++++++++++ .../p04_test_anImmutableFraction.py | 78 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 week2/4-Fractions/p04_anImmutableFraction.py create mode 100644 week2/4-Fractions/p04_test_anImmutableFraction.py diff --git a/week2/4-Fractions/p04_anImmutableFraction.py b/week2/4-Fractions/p04_anImmutableFraction.py new file mode 100644 index 0000000..8f0a27e --- /dev/null +++ b/week2/4-Fractions/p04_anImmutableFraction.py @@ -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) diff --git a/week2/4-Fractions/p04_test_anImmutableFraction.py b/week2/4-Fractions/p04_test_anImmutableFraction.py new file mode 100644 index 0000000..6a0e824 --- /dev/null +++ b/week2/4-Fractions/p04_test_anImmutableFraction.py @@ -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()