Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:04:23+00:00 2026-06-08T18:04:23+00:00

I have been looking without much luck for an implementation of Python that converts

  • 0

I have been looking without much luck for an implementation of Python that converts infix to prefix that ranges on a sufficient amount of arithmetic and logic operators and care about its properties on a good python implementation. More specifically I am interested on the operators that would appear on a conditional clause of a C program. (e.g. it would transform a > 0 && b > 1 in prefix.

Since I am still newbie to Python I would appreciate if anyone could offer me the implementation or some tips on going about this.

I found an implementation around the internet that I lost the reference for (below), but it only cares about the more simple operators. I am a little clueless on how to do this on this version, and if anyone knew a version that already included all the operators I would appreciate to avoid any operator being ignored by accident.

Such implementation should also account for parenthesis.

Please comment if you need more details!

Thank you.

def parse(s):
for operator in ["+-", "*/"]:
    depth = 0
    for p in xrange(len(s) - 1, -1, -1):
        if s[p] == ')': depth += 1
        if s[p] == '(': depth -= 1
        if not depth and s[p] in operator:
            return [s[p]] + parse(s[:p]) + parse(s[p+1:])
s = s.strip()
if s[0] == '(':
    return parse(s[1:-1])
return [s]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:04:24+00:00Added an answer on June 8, 2026 at 6:04 pm

    I don’t quite have time to write an implementation right now, but here is an implementation I wrote that converts infix to postfix (reverse polish) notation (reference: Shunting-yard algorithm). It shouldn’t be too hard to do the modify this algorithm to do prefix instead:

    • ops is the set() of operator tokens.
    • prec is a dict()
      containing operand tokens as keys and an integer for operator
      precedence as it’s values (e.g { "+": 0, "-": 0, "*": 1, "/": 1})
    • Use regular expressions to parse a string into a list of tokens.

    (really, ops and prec could just be combined)

    def infix_postfix(tokens):
        output = []
        stack = []
        for item in tokens:
            #pop elements while elements have lower precedence
            if item in ops:
                while stack and prec[stack[-1]] >= prec[item]:
                    output.append(stack.pop())
                stack.append(item)
            #delay precedence. append to stack
            elif item == "(":
                stack.append("(")
            #flush output until "(" is reached
            elif item == ")":
                while stack and stack[-1] != "(":
                    output.append(stack.pop())
                #should be "("
                print stack.pop()
            #operand. append to output stream
            else:
                output.append(item)
        #flush stack to output
        while stack:
            output.append(stack.pop())
        return output
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking around the Twitter api without much luck. Does anyone know
I have been looking around for a visualization framework that would aid graph visualization
I have been using form inheritance for a while now but without doing much
I have been looking into recursion and TCO. It seems that TCO can make
I have been looking for a function in jquery that will let me just
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking online to find documentation for the function in QTP and
I have been looking into AWS spot instances for some jobs however instead of
I have been looking into backbone.js and I can't seem to figure out how
I have been looking at the Thinktecture.IdentityModel.40 library as a way of handling the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.