This commit is contained in:
ltcptgeneral 2020-04-29 12:58:44 -05:00
parent 07cc2998ee
commit 7134b9f769

View File

@ -2,51 +2,70 @@
"cells": [
{
"cell_type": "code",
"execution_count": 148,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"import re"
"import re\n",
"from decimal import Decimal\n",
"from functools import reduce"
]
},
{
"cell_type": "code",
"execution_count": 155,
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"string = \"3+45+-5\""
]
"source": []
},
{
"cell_type": "code",
"execution_count": 156,
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"def add(string):\n",
" while(len(re.findall(\"[+]{1}[-]?\", string)) != 0):\n",
" string = re.sub(\"\\d+[+]{1}[-]?\\d+\", str(sum([int(i) for i in re.split(\"[+]{1}\", re.search(\"\\d+[+]{1}[-]?\\d+\", string).group())])), string, 1)\n",
" string = re.sub(\"[-]?\\d+[.]?\\d*[+]{1}[-]?\\d+[.]?\\d*\", str(sum([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": 157,
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"def multiply(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": 69,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "'43'"
},
"metadata": {},
"execution_count": 157
"output_type": "stream",
"name": "stdout",
"text": "-29\n"
}
],
"source": [
"add(string)"
"string = \"-3+4+-5*6\"\n",
"string = multiply(string)\n",
"string = add(string)\n",
"print(string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {