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

  • Home
  • SEARCH
  • 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 8903489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:52:56+00:00 2026-06-15T01:52:56+00:00

I am currently working on the python problemsets on a website called singpath. The

  • 0

I am currently working on the python problemsets on a website called singpath. The question is:

Prefix Evaluation
Create a function that evaluates the arithmetic expression in the form of prefix notation without spaces or syntax errors. The expression is given as a string, all the numbers in the expression are integer 0~9, and the operators are +(addition), -(subtraction), *(multiplication), /(division), %(modulo), which operate just the same as those in Python.
Prefix notation, also known as Polish notation, is a form of notation for logic, arithmetic, and algebra. it places operators to the left of their operands. If the arity of the operators is fixed, the result is a syntax lacking parentheses or other brackets that can still be parsed without ambiguity.


This seems simple enough but the string is condensed with no spaces in the input to splice out the data. How could I separate the data from the string without importing modules? Furthermore how could I use the results of the data to solve the given equation? Also please keep in minf that Singpath solutions must be in ONE function that cannot use methods that couldn’t be found in the standard python library. This also includes functions declared within the solution :S

Examples:

>>> eval_prefix("+34")
7
>>> eval_prefix("*−567")
-7
>>> eval_prefix("-*33+2+11")
5
>>> eval_prefix("-+5*+1243")
14
>>> eval_prefix("*+35-72")
40
>>> eval_prefix("%3/52")
1

See my point no spaces D:

  • 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-15T01:52:57+00:00Added an answer on June 15, 2026 at 1:52 am

    OK, not as snazzy as alex jordan’s lamba/reduce solution, but it doesn’t choke on garbage input. It’s sort of a recursive descent parser meets bubble sort abomination (I’m thinking it could be a little more efficient when it finds a solvable portion than just jumping back to the start. 😉

    import operator
    def eval_prefix(expr):
        d = {'+': operator.add,
             '-': operator.sub,
             '*': operator.mul,
             '/': operator.div, # for 3.x change this to operator.truediv
             '%': operator.mod}
        for n in range(10):
            d[str(n)] = n
        e = list(d.get(e, None) for e in expr)
        i = 0
        while i + 3 <= len(e):
            o, l, r = e[i:i+3]
            if type(o) == type(operator.add) and type(l) == type(r) == type(0):
                e[i:i+3] = [o(l, r)]
                i = 0
            else:
                i += 1
        if len(e) != 1:
            print 'Error in expression:', expr
            return 0
        else:
            return e[0]
    
    def test(s, v):
        r = eval_prefix(s)
        print s, '==', v, r, r == v
    
    test("+34", 7)
    test("*-567", -7)
    test("-*33+2+11", 5)
    test("-+5*+1243", 14)
    test("*+35-72", 40)
    test("%3/52", 1)
    test("****", 0)
    test("-5bob", 10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working with Python and have been confused over the fact that
I'm currently working on sprite sheet tool in python that exports the organization into
I have a python program that I am currently working on which is working
The project that I'm currently working on involves translating a website that was written
I'm currently working on a python project, that requires file transmission from a client
I am currently working in Python and my program looks like this: function(1) function(2)
I am currently working on a distributed processing application written in Python that utilises
We're currently working on a python project that basically reads and writes M2M data
I am currently working on a python script that pulls data from a table
I'm currently working on a simple Python script that connects to an IP/Port and

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.