def collect_fractions(fractions): fractions = [list(simplify_fraction(i)) for i in fractions] fractions = common_denominator(fractions) nominators_sum = sum(fraction[0] for fraction in fractions) collected = (nominators_sum, fractions[0][1]) return simplify_fraction(collected)
def test_with_tuple_with_one_integer_in_it_should_raise_error(self): input = (3) exc = None try: simplify_fraction(input) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc),'Tuple with one element from type Integer')
def test_with_tuple_of_elements_different_than_integers_should_raise_error(self): input = (1,'a') exc = None try: simplify_fraction(input) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc),'Tuple with elements different from integers')
def test_with_tuple_with_elements_less_than_two(self): input = ('a') exc = None try: simplify_fraction(input) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc),'Tuple with elements less than two')
def test_with_tuple_with_more_than_two_elements_should_raise_exception(self): input = (2,3,4,5) exc = None try: simplify_fraction(input) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc),'Tuple with more than two elements')
def test_with_denominator_zero_shall_raise_error(self): fraction = (1, 0) exc = None try: simplify_fraction(fraction) except Exception as e: exc = e self.assertIsNotNone(exc) self.assertEqual(str(exc), "Denominator cannot be equal to zero!")
def test_raises_exception_if_denominator_is_zero(self): fraction = (3, 0) exc = None try: simplify_fraction(fraction) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Denominator cannot be zero!')
def test_simplify_fractions(self): fraction1 = (3, 6) fraction2 = (1, 7) fraction3 = (462, 63) result1 = simplify_fraction(fraction1) result2 = simplify_fraction(fraction2) result3 = simplify_fraction(fraction3) self.assertEqual(result1, (1, 2)) self.assertEqual(result2, (1, 7)) self.assertEqual(result3, (22, 3))
def test_with_integers_less_than_zero_should_return_tuple_with_their_absolute(self): input = (-3,9) wanted_result = (-1,3) result = simplify_fraction(input) self.assertEqual(result,wanted_result)
def test_with_correct_arguments_should_return_reduced_tuple(self): input = (3,9) wanted_result = (1,3) result = simplify_fraction(input) self.assertEqual(result,wanted_result)
def collect_fractions(fractions): if check_if_list_is_legit(fractions): lst = expand_the_fractions(fractions) summ = 0 for fraction in lst: nom,denom = fraction summ += nom return simplify_fraction((summ,find_the_total_denominator(fractions)))
def collect_fractions(fractions): if fractions[0][1] == fractions[1][1]: nominator = fractions[0][0] + fractions[1][0] result = nominator, fractions[0][1] return simplify_fraction(result) denominator = lcm(fractions[0][1], fractions[1][1]) multiplier1 = denominator // fractions[0][1] multiplier2 = denominator // fractions[1][1] nominator1 = fractions[0][0] * multiplier1 nominator2 = fractions[1][0] * multiplier2 nominator = nominator1 + nominator2 result = nominator, denominator return simplify_fraction(result)
def test_correct_output(self): self.assertEqual((1, 3), simplify_fraction((3, 9))) self.assertEqual((1, 7), simplify_fraction((1, 7))) self.assertEqual((2, 5), simplify_fraction((4, 10))) self.assertEqual((3, 22), simplify_fraction((63, 462)))
def test_with_float(self): fraction = (3.5, 0.5) result = simplify_fraction(fraction) self.assertEqual(result, (7, 1))
def test_with_only_nominator_or_dominator_negative_shall_return_negative_nominator(self): fraction = (-2, 10) result = simplify_fraction(fraction) self.assertEqual(result, (-1, 5))
def test_with_negative_nominator_and_denominator_shall_reduce_it_like_positive(self): fraction = (-2, -10) result = simplify_fraction(fraction) self.assertEqual(result, (1, 5))