나는 이렇게 학습한다/Algorithm & SQL

0828. Basic Mathematical Operations

daco2020 2022. 8. 28. 22:29
반응형

Your task is to create a function that does four basic mathematical operations.

The function should take three arguments - operation(string/char), value1(number), value2(number). The function should return result of numbers after applying the chosen operation.

Examples(Operator, value1, value2) --> output ('+', 4, 7) --> 11 ('-', 15, 18) --> -3 ('*', 5, 5) --> 25 ('/', 49, 7) --> 7



Solution:

def as_string(func):
    def wrapper(*args):
        return func(*(str(i) for i in args))
    return wrapper

@as_string
def basic_op(operator, value1, value2):
    return eval(value1+operator+value2)


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0830. Switch it Up!  (0) 2022.08.30
0829. Holiday VIII - Duty Free  (0) 2022.08.29
0827. Sum Arrays  (0) 2022.08.27
0826. A wolf in sheep's clothing  (0) 2022.08.26
0825. Find the first non-consecutive number  (0) 2022.08.25