Python program


 
Thread Tools Search this Thread
Top Forums Programming Python program
# 1  
Old 10-30-2015
Python program

Can someone help me please to write a Python program that support a processing of arithmetic expressions in postfix notation.
Expressions in postfix notation contain the operands on which the operation is performed followed by an operator. For example, 3 4 + is equal to 3 + 4 in the infix notation. float numbers and the following operators: + - * / ^ (all float operators) should be supported

There are a variety of ways to implement the postfix notation interpreter. One way is to use stacks. Another would be to use a tree.

The interpreter should accept strings of operators and operands seperated by spaces (no parenthesis) from standard input, and print the final result or output an error if the input is invalid. Each line will be a separate expression. EOF is the signal to quit.

Here is some sample input:

Code:
2.3 4 6.5 * + 3 5 + *
3 4 / 5 6 * - 2 -
3 2 - 2.8 +

Algorithm for evaluating postfix expressions

Start at the first token. For each token:

Code:
If it is an operand, push it on the stack.
Else if it is an operator, then

        pop top value into y

         if operator is binary:

              pop top value into x

             result <- x (oper) y

        else

            result <- (oper) y

       push result onto stack

       fi
fi

Continue until you've reached the end of the expression. There should be exactly one element in the stack; the result of the expression.

If you hit an operator and don't have sufficient operands on the stack (you'd better check) the expression is invalid. If you run out of tokens, and there's more than 1 operand on the stack, the input was invalid.

Simplle output

The result nedds to be printede (and just the result) of each expression, one per line, as each line is evaluated. No other output. So, interactively, this would be used in a very natural way.

If the expression is invalid you will print -E- .

Thanks!

Last edited by Scrutinizer; 10-30-2015 at 04:12 PM.. Reason: Changed around [code] and [/code]
# 2  
Old 10-30-2015
Is this a homework assignment?

Homework assignments must be posted in the Homework & Coursework Forum and must include a completely filled out homework template as specified in that forum's rules.
# 3  
Old 10-30-2015
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Tweak python program to give result in json

Hi , Below is the script which prints result in json but when i validate it has some tab or extra space issues. JSON result { "data": } This is the line I tweaked. Please advise. print "\t{", "\"{#NAME}\":\""+container+hn+"\"}" #!/usr/bin/env python # (2 Replies)
Discussion started by: ashokvpp
2 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Programming

Regarding Python Program with Shell Script

Hi All, I have written a shell script which is using the expect method, it is working fine in terminal window, and then I have executed via python script its also working fine in command prompt functioning properly, I used subprocess.Popen method to execute the shell script file, its working... (0 Replies)
Discussion started by: janaefx
0 Replies

6. AIX

Strange problem running python program from within ant

Hello, I'm currently trying to port an ant based build to AIX 6.1. The build queries the underlying version control system (Mercurial) for some data, which works fine on other platforms (Linux, Solaris). However, on AIX the hg command fails to load python's md5 module when used in the build,... (1 Reply)
Discussion started by: dhs
1 Replies

7. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

8. Shell Programming and Scripting

Shell script to run a python program on multiple entries in a file

Hello I am trying to run a python program using shell script, which takes a single argument from a file. This file has one entry per line : 1aaa 2bbb 3ccc 4ddd 5eee ... ... ... My shell script runs the program, only for the last entry : #!/bin/sh IFS=$'\n' for line in $(cat... (2 Replies)
Discussion started by: ad23
2 Replies

9. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies
Login or Register to Ask a Question