lr6

parent 222df306
def print_results(*args, action=None, result=None):
"""
Вывод в табличном виде результатов вычислений
Функция принимает переменное число значений, которые нужно вывести в табличном виде.
последний аргумент в упакованном кортеже - результат вычислений,
предпоследний - действие, которое выполнилось,остальные — аргументы, с которыми это действие выполнилось.
"""
operands = args[:len(args)]
print('inp_args in pretty mode', operands, action, result)
from string import ascii_lowercase
# print(ascii_lowercase)
def argsPrint(operands):
lst=[]
for i in operands:
lst.append(f"| {i} |")
return lst
def actionPrint(operands, action):
lst=[]
for i in range(operands[0],operands[-1]):
lst.append(f" {i} {action} ")
lst.append(f" {operands[-1]} ")
return lst
arg_list=str((argsPrint(operands)))
action_list=str(actionPrint(operands, action))
st = f"| {arg_list} | {action_list} = {result} |"
print('-' * len(st))
print(st)
print('-' * len(st))
# Пример вывода большого количества аргументов
# inp = 100, 200, 300, action = +
#
# ************************************
# * A, B, C * A + B + C *
# ************************************
# * 100, 200, 300 * 13579 *
# ************************************
from calcprint import print_results
PARAMS = {'precision': 0.00001, 'output_type': float, 'possible_types': (int, float)} PARAMS = {'precision': 0.00001, 'output_type': float, 'possible_types': (int, float)}
...@@ -19,31 +20,31 @@ def calculate(*args, **kwargs): ...@@ -19,31 +20,31 @@ def calculate(*args, **kwargs):
output_type = kwargs['output_type'] output_type = kwargs['output_type']
print(precision) print(precision)
if args[args[-1]] == '+': if args[-1] == '+':
result_sum = sum(args[0:args[-1]]) result_sum = sum(args[0:-1])
if type(result_sum) is not output_type: if type(result_sum) is not output_type:
result_sum = output_type(result_sum) result_sum = output_type(result_sum)
return result_sum return result_sum
if args[args[-1]] == '-': if args[-1] == '-':
result_diff=0 result_diff=0
for n in args[0:args[-1]]: for n in args[0:-1]:
result_diff -= n result_diff -= n
if type(result_diff) is not output_type: if type(result_diff) is not output_type:
result_sum = output_type(result_diff) result_sum = output_type(result_diff)
return result_diff return result_diff
if args[args[-1]] == '*': if args[-1] == '*':
result_mult = 1 result_mult = 1
for n in args[0:args[-1]]: for n in args[0:-1]:
result_mult *= n result_mult *= n
if type(result_mult) is not output_type: if type(result_mult) is not output_type:
result_mult = output_type(result_mult) result_mult = output_type(result_mult)
return round(result_mult, precision) return round(result_mult, precision)
if args[args[-1]] == '/': if args[-1] == '/':
result_division = args[0] result_division = args[0]
for n in args[1:args[-1]]: for n in args[1:-1]:
result_division /= n result_division /= n
if type(result_division) is not output_type: if type(result_division) is not output_type:
result_division = output_type(result_division) result_division = output_type(result_division)
...@@ -63,3 +64,10 @@ def test_packed_calc_sum(): ...@@ -63,3 +64,10 @@ def test_packed_calc_sum():
assert type(calculate(*inp1, action, ** assert type(calculate(*inp1, action, **
PARAMS)) is type(45.0), 'Type of sum is incorrect' PARAMS)) is type(45.0), 'Type of sum is incorrect'
if __name__ == "__main__":
# print(calculate(*list(range(1, 10)), "+", **PARAMS), '\n') # 1 + 2 + 3 + .. + 9
# print(calculate(*list(range(1, 10)), "-", **PARAMS), '\n')
# print(calculate(*list(range(1, 5)), "/", **PARAMS), '\n')
# print(calculate(*[1.0001, 2.2345], "*", **PARAMS), '\n') # 2.23472345
print_results(*list(range(1, 10)), action = "+", result=calculate(*list(range(1, 10)), "+", **PARAMS))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment