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": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 148, "execution_count": 61,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import re" "import re\n",
"from decimal import Decimal\n",
"from functools import reduce"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 155, "execution_count": 56,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": []
"string = \"3+45+-5\""
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 156, "execution_count": 62,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def add(string):\n", "def add(string):\n",
" while(len(re.findall(\"[+]{1}[-]?\", string)) != 0):\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" " return string"
] ]
}, },
{ {
"cell_type": "code", "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": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"output_type": "execute_result", "output_type": "stream",
"data": { "name": "stdout",
"text/plain": "'43'" "text": "-29\n"
},
"metadata": {},
"execution_count": 157
} }
], ],
"source": [ "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": { "metadata": {