tra-analysis/analysis-master/analysis-amd64/analysis/equation.ipynb

125 lines
3.0 KiB
Plaintext
Raw Normal View History

2020-04-29 05:34:16 +00:00
{
"cells": [
{
"cell_type": "code",
2020-04-30 00:01:55 +00:00
"execution_count": 28,
2020-04-29 05:34:16 +00:00
"metadata": {},
"outputs": [],
"source": [
2020-04-29 17:58:44 +00:00
"import re\n",
"from decimal import Decimal\n",
"from functools import reduce"
2020-04-29 05:34:16 +00:00
]
},
{
"cell_type": "code",
2020-04-30 00:01:55 +00:00
"execution_count": 29,
2020-04-29 05:34:16 +00:00
"metadata": {},
"outputs": [],
"source": [
"def add(string):\n",
" while(len(re.findall(\"[+]{1}[-]?\", string)) != 0):\n",
2020-04-29 17:58:44 +00:00
" 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",
2020-04-29 05:34:16 +00:00
" return string"
]
},
{
"cell_type": "code",
2020-04-30 00:01:55 +00:00
"execution_count": 30,
2020-04-29 17:58:44 +00:00
"metadata": {},
"outputs": [],
"source": [
2020-04-30 00:01:55 +00:00
"def sub(string):\n",
" while(len(re.findall(\"\\d+[.]?\\d*[-]{1,2}\\d+[.]?\\d*\", string)) != 0):\n",
" g = re.search(\"\\d+[.]?\\d*[-]{1,2}\\d+[.]?\\d*\", string).group()\n",
" if(re.search(\"[-]{1,2}\", g).group() == \"-\"):\n",
" r = re.sub(\"[-]{1}\", \"+-\", g, 1)\n",
" string = re.sub(g, r, string, 1)\n",
" elif(re.search(\"[-]{1,2}\", g).group() == \"--\"):\n",
" r = re.sub(\"[-]{2}\", \"+\", g, 1)\n",
" string = re.sub(g, r, string, 1)\n",
" else:\n",
" pass\n",
" return string"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def mul(string):\n",
2020-04-29 17:58:44 +00:00
" 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",
2020-04-30 00:01:55 +00:00
"execution_count": 32,
2020-04-29 05:34:16 +00:00
"metadata": {},
"outputs": [
{
2020-04-29 17:58:44 +00:00
"output_type": "stream",
"name": "stdout",
2020-04-30 00:01:55 +00:00
"text": "-30\n"
}
],
"source": [
"def eval(string):\n",
" string = mul(string)\n",
" string = sub(string)\n",
" string = add(string)\n",
" return string"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "-22"
},
"metadata": {},
"execution_count": 34
2020-04-29 05:34:16 +00:00
}
],
"source": [
2020-04-30 00:01:55 +00:00
"string = \"2--3+4-5*6-1\"\n",
"eval(string)"
2020-04-29 05:34:16 +00:00
]
2020-04-30 00:01:55 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2020-04-29 05:34:16 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}