def atoif(nb): try: return int(nb) except ValueError: try: return float(nb) except ValueError: display.quit('Invalid Syntax, use the following form : a*x^p')
def get_reduce_form(original_str): reduced_str = re.sub(r'\s+', '', original_str).replace('x', 'X') splitted_str = reduced_str.split('=') if not reduced_str or len(splitted_str) != 2: display.quit('Invalid Syntax, use the following form : a*x^p') data = extract_data(splitted_str, reduced_str, original_str) reduced_data = reduce_data(data['degrees'], data['numbers']) tools.check_special_case(reduced_data, data) display.reduced_form_and_polynomial_degree(reduced_data, data) tools.check_data_before_processing(data, reduced_data) return reduced_data, max(reduced_data.keys())
def check_syntax(args, checker, elem_nb, check_sign): check = len(checker) + check_sign base = len(args) case1 = re.findall(r'-?\d*\.?\d*\*X\^-?\d*\.?\d+=-?\d*\.?\d*\*X\^-?\d*\.?\d+', args) case2 = re.findall(r'-?\d*\.?\d*\*X\^-?\d*\.?\d+=0', args) if case1 or case2: check += 1 + (elem_nb - 2) if case1 else 2 + (elem_nb - 1) else: display.quit('Invalid Syntax, use the following form : a*x^p') if args[0] == '-': check += 1 if args[args.index('=') + 1] == '-': check += 1 if check != base: display.quit('Invalid Syntax, use the following form : a*x^p')
def check_data_before_processing(data, reduced_data): max_degree = max(reduced_data.keys()) min_degree = min(reduced_data.keys()) if max_degree == 0 and min_degree == 0: display.quit("\n\tThere is no solution for this equation.\n") elif max_degree > 2 and type(max_degree) == int: display.quit("\n\tThe polynomial degree is stricly greater than 2, I can't solve.\n") elif min_degree < 0 and type(min_degree) == int: display.quit("\n\tOne of the polynomial degree is negative, I can't solve.\n") for degree in reduced_data.keys(): if type(degree) == float: display.quit("\n\tOne of the polynomial degree isn't an integer, I can't solve.\n")
def check_special_case(reduced_data, data): if not reduced_data or data['left_side']['degrees'] == data['right_side']['degrees'] and data['left_side']['numbers'] == data['right_side']['numbers']: display.quit("\n\tReduced form: 0 = 0\n\n\tThis is a special case, all real numbers are true for this equation.\n")