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 6070453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:56:06+00:00 2026-05-23T09:56:06+00:00

I am writing a small calculator (with prefix notation) and I’m curious how I’d

  • 0

I am writing a small calculator (with prefix notation) and I’m curious how I’d convert prefix notation to infix notation. I currently have a function, but it’s being weird, and I’m not sure how to fix it. By being weird, I mean that if given ['+', x, y] it will return (() + x + () + y) which is confusing me. Here’s the code.

def pre_in(read):
    #print read
    tempOp = read[0]
    body = read[1:]
    expr = []
    for i in range(len(body)-1):
        if not isinstance(body[i], list) and body[i] != " ":
            expr.append(str(body[i]))
            expr.append(tempOp)
        else:
            expr.append(str(pre_in(body[i])))
            expr.append(tempOp)
    try:
        if not isinstance(body[-1], list):
            expr.append(str(body[-1]))
        else:
            expr.append(str(pre_in(body[-1])))
    except:
        pass
    if expr != None: return "("+' '.join(expr)+")"

What am I doing wrong?

  • 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-05-23T09:56:07+00:00Added an answer on May 23, 2026 at 9:56 am

    Actually your code works fine.

    print pre_in ( ['+', 8, 9] )
    

    yields

    (8 + 9)
    

    EDIT:
    As the others have stated, maybe you want to use a stack. Here a simple sandbox implementation with some examples (it produces many parenthesis but those don’t hurt):

    class Calculator:
        def __init__ (self):
            self.stack = []
    
        def push (self, p):
            if p in ['+', '-', '*', '/']:
                op1 = self.stack.pop ()
                op2 = self.stack.pop ()
                self.stack.append ('(%s %s %s)' % (op1, p, op2) )
            elif p == '!':
                op = self.stack.pop ()
                self.stack.append ('%s!' % (op) )
            elif p in ['sin', 'cos', 'tan']:
                op = self.stack.pop ()
                self.stack.append ('%s(%s)' % (p, op) )
            else:
                self.stack.append (p)
    
        def convert (self, l):
            l.reverse ()
            for e in l:
                self.push (e)
            return self.stack.pop ()
    
    c = Calculator ()
    
    print c.convert ( ['+', 8, 9] )
    print c.convert ( ['!', 42] )
    print c.convert ( ['sin', 'pi'] )
    print c.convert ( ['+', 'sin', '/', 'x', 2, 'cos', '/', 'x', 3] )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where I am reading and writing small blocks of data
I am currently writing a small calendar in ASP.Net C#. Currently to produce the
I'm writing a small program to calculate a physics problem but I'm having problems
I have a small open GL application I'm writing in Java using JOGL. I'm
when writing small functions I often have the case that some parameters are given
I am currently writing a calculator application. I am trying to write a derivative
I'm currenty writing small application for image processing. However I have a big problem
I am writing a small module which will have several different aspects to it,
I have been reading C++ and writing small programs in it for more than
I'm trying to practice my F# by writing small console scripts in F# in

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.