4 kyu

Infix to Postfix Converter

1,134 of 2,675thepyr

Description:

Construct a function that, when given a string containing an expression in infix notation, will return an identical expression in postfix notation.

The operators used will be +, -, *, /, and ^ with left-associativity of all operators but ^.

The precedence of the operators (most important to least) are : 1) parentheses, 2) exponentiation, 3) times/divide, 4) plus/minus.

The operands will be single-digit integers between 0 and 9, inclusive.

Parentheses may be included in the input, and are guaranteed to be in correct pairs.

toPostfix("2+7*5"); // Should return "275*+"
toPostfix("3*3/(7+1)"); // Should return "33*71+/"
toPostfix("5+(6-2)*9+3^(7-1)"); // Should return "562-9*+371-^+"
toPostfix("1^2^3"); // Should return "123^^"
to_postfix("2+7*5") # Should return "275*+"
to_postfix("3*3/(7+1)") # Should return "33*71+/"
to_postfix("5+(6-2)*9+3^(7-1)") # Should return "562-9*+371-^+"
to_postfix("1^2^3") # Should return "123^^"
to_postfix("2+7*5") // Should return "275*+"
to_postfix("3*3/(7+1)") // Should return "33*71+/"
to_postfix("5+(6-2)*9+3^(7-1)") // Should return "562-9*+371-^+"
to_postfix("1^2^3") // Should return "123^^"
to_postfix("2+7*5") # Should return "275*+"
to_postfix("3*3/(7+1)") # Should return "33*71+/"
to_postfix("5+(6-2)*9+3^(7-1)") # Should return "562-9*+371-^+"
to_postfix("1^2^3") # Should return "123^^"
toPostfix "2+7*5" -- Should return "275*+"
toPostfix "3*3/(7+1)" -- Should return "33*71+/"
toPostfix "5+(6-2)*9+3^(7-1)" -- Should return "562-9*+371-^+"
toPostfix "1^2^3" -- Should return "123^^"

You may read more about postfix notation, also called Reverse Polish notation, here: http://en.wikipedia.org/wiki/Reverse_Polish_notation

Mathematics
Algorithms

Stats:

CreatedJan 29, 2014
PublishedJan 29, 2014
Warriors Trained15587
Total Skips6691
Total Code Submissions15444
Total Times Completed2675
JavaScript Completions649
Python Completions1134
C++ Completions714
Ruby Completions68
Haskell Completions69
Rust Completions125
Total Stars513
% of votes with a positive feedback rating91% of 449
Total "Very Satisfied" Votes378
Total "Somewhat Satisfied" Votes61
Total "Not Satisfied" Votes10
Ad
Contributors
  • thepyr Avatar
  • jhoffner Avatar
  • laoris Avatar
  • JohanWiltink Avatar
  • idfumg Avatar
  • FArekkusu Avatar
  • bidouille Avatar
  • user9644768 Avatar
  • user3029010 Avatar
  • rge123 Avatar
  • akar-0 Avatar
Ad