Mathematics
Fundamentals
simple is better that complex, right?
def calculator(a: (int | float) = 0, b: str = '+', c: (int | float) = 0, *, exceptions: bool = False, what: bool = False, using_exec: bool = False): try: return eval(str(a)+str(b)+str(c)) if not using_exec else exec(str(a)+str(b)+str(c)) except ZeroDivisionError as e: if exceptions: raise e else: return float('inf') except Exception as e: if exceptions: raise e else: return float('NaN') finally: if what: return calculator(a^c, '/', a | c + 1, what=True)
def calculator(a,b,c):try: return eval(str(a)+str(b)+str(c))except ZeroDivisionError: return float('inf')- def calculator(a: (int | float) = 0, b: str = '+', c: (int | float) = 0, *, exceptions: bool = False, what: bool = False, using_exec: bool = False):
- try:
- return eval(str(a)+str(b)+str(c)) if not using_exec else exec(str(a)+str(b)+str(c))
- except ZeroDivisionError as e:
- if exceptions: raise e
- else: return float('inf')
- except Exception as e:
- if exceptions: raise e
- else: return float('NaN')
- finally:
- if what: return calculator(a^c, '/', a | c + 1, what=True)
test.assert_equals(calculator(1, "/", 0), float('inf')) test.assert_equals(calculator(42, "+", 11), 53) test.assert_equals(calculator(6, "-", 50), -44) test.assert_equals(calculator(32, "/", 2), 16) test.assert_equals(calculator(25, "*", 5), 125) test.assert_equals(calculator(11, '%', 5), 1) test.assert_equals(calculator(1, "/", 0, using_exec=True), float('inf'))
test.assert_equals(calculator(1,"/", 0), float('inf'))test.assert_equals(calculator(42,"+", 11), 53)- test.assert_equals(calculator(1, "/", 0), float('inf'))
- test.assert_equals(calculator(42, "+", 11), 53)
- test.assert_equals(calculator(6, "-", 50), -44)
- test.assert_equals(calculator(32, "/", 2), 16)
- test.assert_equals(calculator(25, "*", 5), 125)
- test.assert_equals(calculator(11, '%', 5), 1)
- test.assert_equals(calculator(1, "/", 0, using_exec=True), float('inf'))
Mathematics
Fundamentals
bruh...
i dunno how to use kumite, i think, i just need to rewrite this code...
what?
def calculator(sign:str = '+', n1:(float | int) = 0, n2:(float | int) = 0, exceptions_allowed: bool = False): """The simplest calculator ever. First argument is sign, +, -, *, /, ** and % supported. Default is +. The next two arguments is n1 and n2. Integers, Floats and Complex numbers supported. Their defaults is 0. Other arguments is options. exceptions_allowed - if True, will return exception instead of 0 when error occurs. Default is False.""" if not isinstance(sign, str): if not exceptions_allowed: return 0 else: raise TypeError('Type of the sign must be string.') if not isinstance(n1, (int, float, complex)): if not exceptions_allowed: return 0 else: raise TypeError('Type of the first number must be int, float or complex.') if not isinstance(n2, (int, float, complex)): if not exceptions_allowed: return 0 else: raise TypeError('Type of the second number must be int, float or complex.') if sign not in "+-*/**%": if not exceptions_allowed: return 0 else: raise TypeError('Sign must be +, -, *, /, ** or %.') if sign == "/" and n2 == 0: if not exceptions_allowed: return float('inf') #mathematically this is better, than x/0=0 else: raise ZeroDivisionError('Can\'t divide by zero.') return eval(str(n1) + sign + str(n2)) #using universal and optimized, but dangerous and bugged solution
def Calculator(*args):- def calculator(sign:str = '+', n1:(float | int) = 0, n2:(float | int) = 0, exceptions_allowed: bool = False):
- """The simplest calculator ever.
- First argument is sign, +, -, *, /, ** and % supported. Default is +.
- The next two arguments is n1 and n2. Integers, Floats and Complex numbers supported. Their defaults is 0.
if not (str(args[0]) in "+-*/" and len(args) == 3):return 0sign, n1, n2 = args- Other arguments is options.
- exceptions_allowed - if True, will return exception instead of 0 when error occurs. Default is False."""
- if not isinstance(sign, str):
- if not exceptions_allowed: return 0
- else: raise TypeError('Type of the sign must be string.')
- if not isinstance(n1, (int, float, complex)):
- if not exceptions_allowed: return 0
- else: raise TypeError('Type of the first number must be int, float or complex.')
- if not isinstance(n2, (int, float, complex)):
- if not exceptions_allowed: return 0
- else: raise TypeError('Type of the second number must be int, float or complex.')
- if sign not in "+-*/**%":
- if not exceptions_allowed: return 0
- else: raise TypeError('Sign must be +, -, *, /, ** or %.')
- if sign == "/" and n2 == 0:
return 0return n1 + n2 if sign == "+" else n1 - n2 if sign == "-" else n1 * n2 if sign == "*" else n1 / n2 if sign == "/" else 0- if not exceptions_allowed: return float('inf') #mathematically this is better, than x/0=0
- else: raise ZeroDivisionError('Can\'t divide by zero.')
- return eval(str(n1) + sign + str(n2)) #using universal and optimized, but dangerous and bugged solution
test.assert_equals(calculator("/", 1, 0), float('inf')) test.assert_equals(calculator("+", 42, 11), 53) test.assert_equals(calculator("-", 6, 50), -44) test.assert_equals(calculator("/", 32, 2, exceptions_allowed=True), 16) test.assert_equals(calculator("*", 25, 5), 125) test.assert_equals(calculator(123, 2), 0) test.assert_equals(calculator('%', 11, 5), 1)
test.assert_equals(Calculator("/", 1, 0), 0)test.assert_equals(Calculator("+", 42, 11), 53)test.assert_equals(Calculator("-", 6, 50), -44)test.assert_equals(Calculator("/", 32, 2), 16)test.assert_equals(Calculator("*", 25, 5), 125)test.assert_equals(Calculator(123, 2), 0)- test.assert_equals(calculator("/", 1, 0), float('inf'))
- test.assert_equals(calculator("+", 42, 11), 53)
- test.assert_equals(calculator("-", 6, 50), -44)
- test.assert_equals(calculator("/", 32, 2, exceptions_allowed=True), 16)
- test.assert_equals(calculator("*", 25, 5), 125)
- test.assert_equals(calculator(123, 2), 0)
- test.assert_equals(calculator('%', 11, 5), 1)