postfix-notation

Simplification Algorithm for Reverse Polish Notation

荒凉一梦 提交于 2019-12-01 11:46:31
问题 A couple of days ago I played around with Befunge which is an esoteric programming language. Befunge uses a LIFO stack to store data. When you write programs the digits from 0 to 9 are actually Befunge-instructions which push the corresponding values onto the stack. So for exmaple this would push a 7 to stack: 34+ In order to push a number greater than 9, calculations must be done with numbers less than or equal to 9. This would yield 123. 99*76*+ While solving Euler Problem 1 with Befunge I

Why is JavaScript's postfix notation different from C and Perl?

て烟熏妆下的殇ゞ 提交于 2019-11-30 20:39:19
I'm studying for an exam on JavaScript at the moment. I've also got a little knowledge of C and Perl so I'm familiar with prefix and postfix notation in all three languages. I did an online practice exam for it and one mistake I made was in evaluating the following code: var x = 10; x += x--; Now, I thought it would evaluate to 19 because it would be 10 + 10, then subtract 1 to make 9. But the feedback I got was that it was wrong and it actually evaluates to 20. I thought that sounded a bit suspicious so I tested it out in an HTML document, and it came out with 20 again. I then tried the

Postfix to Infix with minimum number of parentheses

杀马特。学长 韩版系。学妹 提交于 2019-11-30 16:28:38
I am looking for algorithm postfix to infix notation which will produce the minimum number of the parentheses. I have found that but it will produce many, many parentheses: http://tajendrasengar.blogspot.com/2011/09/postfix-to-infix-algorithm.html For example The input: <ONP>abcd*/+~ The result: <INF>~(a+b/(c*d)) What you need to do if you really want as few parentheses as possible, is similar to what the algorithm you linked to says. However... You should store an operator for each composite operand in the Stack . Namely, the last operator used in the operand. You could use a second Stack for

Why is JavaScript's postfix notation different from C and Perl?

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:55:20
问题 I'm studying for an exam on JavaScript at the moment. I've also got a little knowledge of C and Perl so I'm familiar with prefix and postfix notation in all three languages. I did an online practice exam for it and one mistake I made was in evaluating the following code: var x = 10; x += x--; Now, I thought it would evaluate to 19 because it would be 10 + 10, then subtract 1 to make 9. But the feedback I got was that it was wrong and it actually evaluates to 20. I thought that sounded a bit

Can this Python postfix notation (reverse polish notation) interpreter be made more efficient and accurate?

余生长醉 提交于 2019-11-28 21:58:44
Here is a Python postfix notation interpreter which utilizes a stack to evaluate the expressions. Is it possible to make this function more efficient and accurate? #!/usr/bin/env python import operator import doctest class Stack: """A stack is a collection, meaning that it is a data structure that contains multiple elements. """ def __init__(self): """Initialize a new empty stack.""" self.items = [] def push(self, item): """Add a new item to the stack.""" self.items.append(item) def pop(self): """Remove and return an item from the stack. The item that is returned is always the last one that

Postfix expression list evaluation

旧时模样 提交于 2019-11-28 11:07:20
问题 I have written a program to evaluate a post-fix expression in prolog recursively from an expression list. For example, given the following list: [+,1,2] It should return 3. They way I have constructed my predicate is to call itself recursively until it reaches the end of the list so that it reads values backwards. (the same as reading this list from left to right:[2,1,+]). My problem is that when I try to return more than one value through the recursive calls all the values suddenly disappear

Can this Python postfix notation (reverse polish notation) interpreter be made more efficient and accurate?

懵懂的女人 提交于 2019-11-27 21:02:23
问题 Here is a Python postfix notation interpreter which utilizes a stack to evaluate the expressions. Is it possible to make this function more efficient and accurate? #!/usr/bin/env python import operator import doctest class Stack: """A stack is a collection, meaning that it is a data structure that contains multiple elements. """ def __init__(self): """Initialize a new empty stack.""" self.items = [] def push(self, item): """Add a new item to the stack.""" self.items.append(item) def pop(self)

Handling extra operators in Shunting-yard

落爺英雄遲暮 提交于 2019-11-27 06:21:55
问题 Given an input like this: 3+4+ Algorithm turns it in to 3 4 + + I can find the error when it's time to execute the postfix expression. But, is it possible to spot this during the conversion? (Wikipedia article and internet articles I've read do not handle this case) Thank you 回答1: Valid expressions can be validated with a regular expression, aside from parenthesis mismatching. (Mismatched parentheses will be caught by the shunting-yard algorithm as indicated in the wikipedia page, so I'm