Sponsored Content
Full Discussion: Python program
Top Forums Programming Python program Post 302959249 by Jim-266 on Friday 30th of October 2015 02:52:24 PM
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]
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
POSTFIX-ADD-FILTER(8)					      System Manager's Manual					     POSTFIX-ADD-FILTER(8)

NAME
postfix-add-filter - add filter service to Postfix master.cf SYNOPSIS
postfix-add-filter [smtp service name...] [smtpd port...] DESCRIPTION
The postfix-add-filter(8) command adds an smtp service named smtp service name and and smtpd server listening on smtpd port to etc/post- fix/master.cf to facilitate integration of SMTP filters such as amavisd-new or clamsmtp. The configuration is based on the upstream recom- mendations for amavisd-new 2.6.0. Adminstrators should verify it is appropriate for their requirements. The original file is copied prior to modification and left in /etc/postfix to make it possible to revert changes easily. Available in the Debian package for Postfix version 2.5.3 and later. DIAGNOSTICS
If the given smtp service name or smtpd port already appear in the master.cf, a message will be printed to standard out and master.cf will not be modified. ENVIRONMENT
MAIL_CONFIG Directory with Postfix configuration files. The postfix-add-filter(8) command should use this, but it currently doesn't. It is hard coded to /etc/postfix. This should be changed. CONFIGURATION PARAMETERS
None FILES
/etc/postfix/master.cf SEE ALSO
postconf(5), Postfix configuration LICENSE
This software is licensed under the MIT open source license. AUTHOR(S) Scott Kitterman <scott@kitterman.com> POSTFIX-ADD-FILTER(8)
All times are GMT -4. The time now is 09:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy