0

I'm a student. Few days ago I think that I can make a python program to solve math formula and after few minute coding I ended up like this.

class Formula:
    def __init__(self,formula):
        self.formula=formula
    def formulate(self,values):
        vals=values.split(";")
        for i in vals:
            reval=i.split('=')
            self.formula=self.formula.replace(reval[0],reval[1])
        return eval(self.formula)
sum=Formula('(a+b)/c')
print(sum.formulate('a=2;b=56;c=5'))

How can I make this program to perform better? Can I make this program to solve problem step-by-step?How?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    What do you mean by step by step? Can you give expected input and output for reference? – adityanithariya Sep 27 '21 at 15:47
  • I mean to ask Can I make step by step problem solver like this? – Tara Nath Niraula Sep 27 '21 at 15:48
  • You should check out [SymPy](https://www.sympy.org/en/index.html). – 0x5453 Sep 27 '21 at 15:48
  • 2
    It's still not clear what you mean by "step by step" to me... Do you mean something like this: `(2 + 56)/5 = (58)/5 = final_number`? – ForceBru Sep 27 '21 at 15:52
  • Parsing of mathematical formulas is a non-trivial problem. Evaluating the resulting things in a step by step manner is equivalent to walking the resulting expression tree in a certain way. The `ast` module provides ways to construct expression trees from well-formed expressions. I suggest you work through a tutorial about that module if you want to go that route. – John Coleman Sep 27 '21 at 16:09
  • 1
    This might help https://stackoverflow.com/questions/40639652/tracing-python-expression-evaluation-step-by-step – Balaji Sep 27 '21 at 16:13
  • @ForceBru yes like that : ```(2 + 56)/5 = (58)/5 = final_number``` – Tara Nath Niraula Sep 27 '21 at 17:43
  • 1
    This is more suited to [codereview.se] – Sayse Sep 28 '21 at 09:01

1 Answers1

1

This is a interesting question. If you want to output the intermediate expression then simple string replacement and calling Python evaluation won't work.

You need to:

  1. Parse the expression string to a binary tree, with leaf nodes being the variables and other nodes being operators
  2. Calculate for the value using the tree, starting from root. You go deep reclusively (DFS) and once an operator node has got a value you output the new express string with the tree.
  3. To turn a tree into an expression string you need to traverse the binary tree in middle order.
  4. You also need to deal with operator priority and parentheses in the previous steps.

P.S. I wrote a simple arithmetic evaluation script for fun. Play with it and have fun.

Medo Paw
  • 81
  • 5
  • Can you give me an example.I'm new at programming – Tara Nath Niraula Sep 27 '21 at 17:27
  • @TaraNathNiraula, this problem is really non-trivial, so it may not be suited for someone new to programming. However, it is possible to do, and may lead to things like common subexpression analysis and compilers in general – ForceBru Sep 27 '21 at 17:50
  • please tech me the basics – Tara Nath Niraula Sep 27 '21 at 17:51
  • 1
    @TaraNathNiraula Stack Overflow is a site for focused questions which admit specific answers. You are essentially asking for a custom tutorial. That is too broad for this site. – John Coleman Sep 27 '21 at 17:57
  • @TaraNathNiraula As for tutorials you can check http://blog.erezsh.com/how-to-write-a-calculator-in-50-python-lines-without-eval/ and http://blog.erezsh.com/how-to-write-a-calculator-in-70-python-lines-by-writing-a-recursive-descent-parser/ – Medo Paw Sep 30 '21 at 00:06
  • @TaraNathNiraula I wrote a [simple arithmetic evaluation script](https://gist.github.com/medopaw/b241b58520eb7bf22f181b13ba4fb885#file-eval-py) for fun. Play with it and have fun. – Medo Paw Oct 06 '21 at 03:37