Help with Python: Implementing Algorithm

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

The discussion focuses on implementing an algorithm in Python for evaluating infix expressions, specifically using functions like eval_infix_sum, eval_infix_product, and eval_infix_factor. The user initially struggled with handling operations and parentheses but made progress by converting string inputs to integers. The conversation emphasizes the importance of recursion and function calls to manage operator precedence and expression evaluation, ultimately leading to a structured approach for parsing and calculating results from infix expressions.

PREREQUISITES
  • Understanding of Python functions and syntax, particularly the def keyword.
  • Familiarity with recursion and iterative loops in programming.
  • Knowledge of operator precedence in mathematical expressions.
  • Experience with list data structures in Python.
NEXT STEPS
  • Implement a complete version of eval_infix_factor to handle parentheses correctly.
  • Research Python's error handling to manage invalid expressions effectively.
  • Explore the use of regular expressions for parsing complex infix expressions.
  • Learn about abstract syntax trees (AST) for a more structured approach to expression evaluation.
USEFUL FOR

Students learning Python, software developers working on expression parsers, and anyone interested in algorithm design for mathematical computations.

  • #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