def test_str_representation_of_term_is_correct(self): with self.subTest('Coefficient is bigger than 1'): t = Term(coeff=2, variable='x', power=3) expected = '2*x^3' self.assertEqual(expected, str(t)) with self.subTest('Coefficient is 1'): t = Term(coeff=1, variable='x', power=3) expected = 'x^3' self.assertEqual(expected, str(t)) with self.subTest('Power is 1'): t = Term(coeff=1, variable='x', power=1) expected = 'x' self.assertEqual(expected, str(t)) with self.subTest('Coeff is bigger than 1, power is 1'): t = Term(coeff=2, variable='x', power=1) expected = '2*x' self.assertEqual(expected, str(t)) with self.subTest('Term is constant'): t = Term.constant(2) expected = '2' self.assertEqual(expected, str(t))
def test_polynomial_str_representation_is_as_expected_with_more_than_one_term( self): term_obj1 = Term([3, True, 2]) term_obj2 = Term([1, True, None]) test_obj = Polynomial([term_obj1, term_obj2]) self.assertEqual(str(test_obj), '3x^2+1x')
def test_polynomial_init_initializes_objects_as_expected(self): term_obj1 = Term([1, True, 2]) term_obj2 = Term([1, True, None]) test_obj = Polynomial([term_obj1, term_obj2]) self.assertEqual(getattr(test_obj, 'terms_list'), [term_obj1, term_obj2])
def test_derivate_returns_coefficient_only_when_x_raised_to_the_power_of_1(self): string = '2x' t = Term(string) result = t.derivate() expected = '2' self.assertEqual(result, expected)
def test_derivate_with_a_full_term(self): string = '2x6' t = Term(string) result = t.derivate() expected = '12x^5' self.assertEqual(result, expected)
def test_polynomial_derivative_calculates_derivative_as_expected_with_more_than_one_term( self): term_obj1 = Term([3, True, 2]) term_obj2 = Term([8, True, None]) test_obj = Polynomial([term_obj1, term_obj2]) self.assertEqual(test_obj.derivative(), '6x+8')
def test_term_validate_term_raises_exception_if_length_of_list_is_not_three( self): test_list = [1, True, 3, 5] exc = None try: Term.validate_term(test_list) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'List must contain 3 elements.')
def test_term_validate_term_raises_exception_if_argument_not_of_list_type( self): test_list = (1, True, 3) exc = None try: Term.validate_term(test_list) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Argument must be of "list" type.')
def test_term_validate_term_raises_exception_if_second_elem_of_list_not_bool_or_none( self): test_list = [1, 'False', 3] exc = None try: Term.validate_term(test_list) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Second element of list must be of "bool" type.')
def test_term_validate_term_raises_exception_if_third_elem_of_list_not_int_or_none( self): test_list = [1, True, '3'] exc = None try: Term.validate_term(test_list) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual( str(exc), 'Third element of list must be of "int" type or None.')
def test_is_constant(self): with self.subTest('Term with coeff, variable and power is not'): t = Term(coeff=2, variable='x', power=3) self.assertFalse(t.is_constant) with self.subTest('Constant term is constant'): value = 2 t = Term.constant(value) self.assertTrue(t.is_constant) self.assertEqual(value, t.coeff) self.assertIsNone(t.variable) self.assertEqual(0, t.power)
def test_terms_are_sorted_by_power(self): with self.subTest('Case 1'): terms = [ Term(coeff=1, variable='x', power=2), Term(coeff=1, variable='x', power=5) ] p = Polynomial(terms) expected_terms = [ Term(coeff=1, variable='x', power=5), Term(coeff=1, variable='x', power=2) ] self.assertEqual(expected_terms, p.terms)
def test_polynomial_derivative_calculates_derivative_as_expected_with_one_term( self): term_obj1 = Term([3, True, 2]) test_obj = Polynomial([term_obj1]) self.assertEqual(test_obj.derivative(), '6x')
def test_term_init_initializes_objects_as_expected(self): test_list = [1, True, 3] test_obj = Term(test_list) self.assertEqual(getattr(test_obj, 'coefficient'), 1) self.assertEqual(getattr(test_obj, 'variable'), True) self.assertEqual(getattr(test_obj, 'power'), 3)
def test_throwing_an_error_if_init_is_passed_something_other_than_string(self): string = [] exc = None try: t = Term(string) except Exception as err: exc = err self.assertEqual(str(exc), f'Term\'s argument must be a string, not a {type(string)}')
def test_polynomial_validate_terms_list_raises_exception_if_element_is_not_of_term_type( self): exc = None term_obj1 = Term([3, True, 2]) try: test_obj = Polynomial([term_obj1, (1, 2, 3)]) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Elements must be of "Term" type')
def test_derivative_function_return_type(self): parsed_strings_list = parser('2x^3 + 3x + 1') terms_list = [] for i in parsed_strings_list: terms_list.append(Term(i)) poli = Polynomial(terms_list) result = type(poli.derivative()) expected = list self.assertEqual(result, expected)
def test_derivative_function(self): parsed_strings_list = parser('2x^3 + 3x + 1') terms_list = [] for i in parsed_strings_list: terms_list.append(Term(i)) poli = Polynomial(terms_list) result = poli.derivative() expected = ['6x^2', '3'] self.assertEqual(result, expected)
def main(): validate_call(sys.argv) my_list = parser(sys.argv[1]) term_list = [] for elem in my_list: term = term_splitter(elem) term_list.append(Term(term)) function = Polynomial(term_list) print(f"The derivative of f(x) = {function}") print(f"f'(x) = {function.derivative()}")
def print_derivative(string=sys.argv[1]): parsed_strings_list = parser(string) operators_list = operators_extractor(string) terms_list = [] for i in parsed_strings_list: terms_list.append(Term(i)) poli = Polynomial(terms_list) derivatives_list = poli.derivative() solution = '' for index in range(len(derivatives_list)): solution += derivatives_list[index] if index != len(derivatives_list) - 1: solution += operators_list[index] if solution == '': solution = '0' return f"Derivative of f(x) = {string} is:\nf'(x) = {solution}"
def test_term_repr_representation_is_as_expected_with_coeffiecient_variable_and_power( self): test_list = [2, True, 5] test_obj = Term(test_list) self.assertEqual(repr(test_obj), '2x^5')
def test_coefficient_and_power_assignment(self): string = '2x3' t = Term(string) self.assertEqual(t.coefficient, 2) self.assertEqual(t.power, 3)
def test_term_str_representation_is_as_expected_with_only_coefficient( self): test_list = [1, False, None] test_obj = Term(test_list) self.assertEqual(str(test_obj), '1')
def test_term_derivative_is_calculated_as_expected_with_only_variable_and_power( self): test_list = [None, True, 3] test_obj = Term(test_list) self.assertEqual(test_obj.derivative(), '3x^2')
def test_term_derivative_is_calculated_as_expected_with_only_coefficient( self): test_list = [1, False, None] test_obj = Term(test_list) self.assertEqual(test_obj.derivative(), '0')
def test_term_validate_term_passes_with_correct_input(self): test_list = [1, True, 3] Term.validate_term(test_list)
def test_term_derivative_is_calculated_expected_with_only_coefficient_and_variable( self): test_list = [3, True, None] test_obj = Term(test_list) self.assertEqual(test_obj.derivative(), '3')
def test_term_derivative_is_calculated_as_expected_with_coeffiecient_variable_and_power( self): test_list = [2, True, 5] test_obj = Term(test_list) self.assertEqual(test_obj.derivative(), '10x^4')
def test_polynomial_validate_terms_list_passes_with_correct_input(self): term_obj1 = Term([3, True, 2]) term_obj2 = Term([8, True, None]) test_obj = Polynomial([term_obj1, term_obj2])
def test_polynomial_str_representation_is_as_expected_with_one_term(self): term_obj1 = Term([3, True, 2]) test_obj = Polynomial([term_obj1]) self.assertEqual(str(test_obj), '3x^2')