2020-04-29 05:34:16 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
2020-04-29 05:35:19 +00:00
"execution_count": 155,
2020-04-29 05:34:16 +00:00
"metadata": {},
"outputs": [],
"source": [
"string = \"3+45+-5\""
]
},
{
"cell_type": "code",
2020-04-29 05:35:19 +00:00
"execution_count": 156,
2020-04-29 05:34:16 +00:00
"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",
" return string"
]
},
{
"cell_type": "code",
2020-04-29 05:35:19 +00:00
"execution_count": 157,
2020-04-29 05:34:16 +00:00
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
2020-04-29 05:35:19 +00:00
"text/plain": "'43'"
2020-04-29 05:34:16 +00:00
},
"metadata": {},
2020-04-29 05:35:19 +00:00
"execution_count": 157
2020-04-29 05:34:16 +00:00
}
],
"source": [
"add(string)"
]
}
],
"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
}