This commit is contained in:
ltcptgeneral 2020-04-29 22:34:43 -05:00
parent 60ffe7645b
commit 429d3eb42c

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 48,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -13,7 +13,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 49,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -25,7 +25,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 50,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -45,7 +45,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 51,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -57,17 +57,37 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 52,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"output_type": "stream",
"name": "stdout",
"text": "-30\n"
}
],
"source": [ "source": [
"def eval(string):\n", "def div(string):\n",
" while(len(re.findall(\"[/]{1}[-]?\", string)) != 0):\n",
" string = re.sub(\"[-]?\\d+[.]?\\d*[/]{1}[-]?\\d+[.]?\\d*\", str(reduce((lambda x, y: x / y), [Decimal(i) for i in re.split(\"[/]{1}\", re.search(\"[-]?\\d+[.]?\\d*[/]{1}[-]?\\d+[.]?\\d*\", string).group())])), string, 1)\n",
" return string"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"def exp(string):\n",
" while(len(re.findall(\"[\\^]{1}[-]?\", string)) != 0):\n",
" string = re.sub(\"[-]?\\d+[.]?\\d*[\\^]{1}[-]?\\d+[.]?\\d*\", str(reduce((lambda x, y: x ** y), [Decimal(i) for i in re.split(\"[\\^]{1}\", re.search(\"[-]?\\d+[.]?\\d*[\\^]{1}[-]?\\d+[.]?\\d*\", string).group())])), string, 1)\n",
" return string"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"def evaluate(string):\n",
" string = exp(string)\n",
" string = div(string)\n",
" string = mul(string)\n", " string = mul(string)\n",
" string = sub(string)\n", " string = sub(string)\n",
" string = add(string)\n", " string = add(string)\n",
@ -76,29 +96,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 70,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "-128+0.8\n"
},
{ {
"output_type": "execute_result", "output_type": "execute_result",
"data": { "data": {
"text/plain": "-22" "text/plain": "'-127.2'"
}, },
"metadata": {}, "metadata": {},
"execution_count": 34 "execution_count": 70
} }
], ],
"source": [ "source": [
"string = \"2--3+4-5*6-1\"\n", "string = \"8^2*4/-2+0.8\"\n",
"eval(string)" "evaluate(string)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {