r/dailyprogrammer • 1 2 • Oct 18 '12

[10/18/2012] Challenge #104 [Hard] (Stack Attack)

Description:

You are a devilish engineer designing a new programming language titled D++, which stands for a "Dijkstra++", named after your favorite computer scientist. You are currently working on the math-operations parsing component of your interpreter: though the language only supports infix-notation (such as "1 + 2 * 3"), your interpreter internals require you to represent all math strings in reverse polish notation (for easier, stack-based, computing). Your goal is to simply take a guaranteed valid infix-notation string of math operations and generate (printing it out) an equivalent and valid reverse polish notation string.

Formal Inputs & Outputs:

Input Description:

string MathOperations - A valid string of infix math notation.

Output Description:

Print the converted RPN form of the given math operations string.

Sample Inputs & Outputs:

"1 + 2" should be printed as "1 2 +". "(1+2)*3" should be printed as "3 2 1 + *". "(6 (7 – 2)) / 3 + 9 * 4" should be printed as "6 7 2 - * 3 / 9 4 * +".

Notes:

Do not get trapped in overly-complex solutions; there are formal solutions, though many ways of solving for this problem. Check out the Shunting Yard Algorithm for details on how to convert math-operation strings (a stack of tokens) from one notation system to another.

21 Upvotes

15 comments sorted by

View all comments

1

u/robin-gvx 0 2 Oct 19 '12 edited Oct 19 '12

Modified my solution for a different challenge: (which was to write a calculator)

is-number s:
	try:
		drop to-num s
		true
	catch value-error:
		false

set :prec { "+" 1 "-" 1 "*" 2 "/" 2 }

shunting-yard text:
	local :output-queue [] # it's implemented by using a stack and reversing it when done
	local :stack []
	for c in chars text:
		if is-number c:
			push-to output-queue c
		elseif = "(" c:
			push-to stack c
		elseif = ")" c:
			while != "(" dup pop-from stack:
				push-to output-queue
			drop
		else: #just assume it's an operator
			local :v true
			while and stack v:
				local :o2 pop-from stack
				if = "(" o2:
					push-to stack o2
					set :v false
				else:
					if <= get-from prec c get-from prec o2:
						push-to output-queue o2
					else:
						push-to stack o2
						set :v false
			push-to stack c
	for op in stack:
		push-to output-queue op
	join " " reversed output-queue

print "Please enter a calculation:"
print shunting-yard input