Help with Python: Implementing Algorithm

  • Context: Python 
  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around implementing an algorithm in Python for evaluating mathematical expressions represented as lists of strings. Participants explore how to handle operations such as addition, subtraction, multiplication, and division, as well as the evaluation of parenthesized sub-expressions. The focus is on the technical implementation details and the interaction between various functions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant shares a sample code for evaluating expressions and seeks help on how to implement the algorithm in Python.
  • Another participant proposes a method to handle addition and subtraction but encounters issues with string concatenation instead of numerical addition.
  • A later reply indicates a solution to convert strings to integers for proper arithmetic operations.
  • Participants discuss the need for the "pos" variable to track the current position in the expression list and how it relates to function calls.
  • Some participants suggest using recursion or a while loop to evaluate expressions until a termination token is reached.
  • One participant outlines a pseudo code structure for handling sum and multiplication expressions, including parenthesized expressions.
  • Another participant realizes how the input string will be processed into a list and subsequently evaluated by the defined functions.

Areas of Agreement / Disagreement

Participants express various approaches to implementing the algorithm, with no consensus on a single method. There are multiple competing views on how to structure the evaluation process, particularly regarding the use of recursion versus loops.

Contextual Notes

Participants note that the implementation must account for the correct tracking of positions within the expression list and the handling of parenthetical expressions, which adds complexity to the evaluation process.

  • #31
Working solution:
Code:
def eval_infix_sum(expr,pos):
    ans, pos = eval_infix_product(expr,pos)
    while expr[pos] == '+' or expr[pos] == '-':
        if expr[pos] == '+':
            rhs,pos = eval_infix_product(expr,pos + 1)
            ans = ans + rhs
        elif expr[pos] == '-':
            rhs,pos = eval_infix_product(expr,pos + 1)
            ans = ans - rhs
    return ans,pos
def eval_infix_product(expr,pos):
    ans,pos = eval_infix_factor(expr,pos)
    while expr[pos] == '*' or expr[pos] == '/' or expr[pos] == '%':
        if expr[pos] == '*':
            rhs,pos = eval_infix_factor(expr,pos + 1)
            ans = ans * rhs
        elif expr[pos] == '/':
            rhs,pos = eval_infix_factor(expr,pos + 1)
            ans = ans / rhs
        elif expr[pos] == '%':
            rhs,pos = eval_infix_factor(expr,pos + 1)
            ans = ans % rhs
    return ans,pos
def eval_infix_factor(expr,pos):
    if expr[pos] == '(':
        ans,pos = eval_infix_sum(expr,pos+1)
        return ans,pos+1
    else:
        return int(expr[pos]),pos+1
def eval_infix_list(expr):
    ans,discard = eval_infix_sum(expr,0)
    return ans
def eval_infix(expr):
    return eval_infix_list(expr.split() + [ ";"])
Working on making it support minus(es?):p
 
Technology news on Phys.org
  • #32
Looks good! (Yes)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
1
Views
2K