Skip to content

Instantly share code, notes, and snippets.

@technillogue
Forked from anonymous/gist:5882172
Last active February 22, 2022 18:20
Show Gist options
  • Save technillogue/5882175 to your computer and use it in GitHub Desktop.
Save technillogue/5882175 to your computer and use it in GitHub Desktop.
A simple python calculator using lambda expressions
operations = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"/": lambda x, y: x / y,
"*": lambda x, y: x * y
}
def calculate(expr):
numxChars = ""
operation = None
numyChars = ""
for char in expr:
if char.isdigit():
if operation is None:
numxChars += char
else:
numyChars += char
elif char in operations:
operation = char
elif char.isspace:
pass
else:
raise Exception("invalid charecter: " + char)
return operations[operation](int(numxChars), int(numyChars))
print calculate(raw_input("Input? "))
$ python calc.py
Input? 1 + 1
2
Input? 2/1
2
Input? 3-4
-1
Input? 7*5
35
@khanhnnvn
Copy link

Can i using this case for big number have 1000 charactor?

@Pawka
Copy link

Pawka commented Apr 11, 2018

@akash-mahanty it does not support parenthesis. Quite poor implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment