{ "cells": [ { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "import re\n", "from decimal import Decimal\n", "from functools import reduce" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def add(string):\n", " while(len(re.findall(\"[+]{1}[-]?\", string)) != 0):\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": 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": "stream", "name": "stdout", "text": "-29\n" } ], "source": [ "string = \"-3+4+-5*6\"\n", "string = multiply(string)\n", "string = add(string)\n", "print(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 }