Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jtwebman/infix-to-prefix
Function that given an infix arithmetic expression, outputs a prefix version
https://github.com/jtwebman/infix-to-prefix
Last synced: 11 days ago
JSON representation
Function that given an infix arithmetic expression, outputs a prefix version
- Host: GitHub
- URL: https://github.com/jtwebman/infix-to-prefix
- Owner: jtwebman
- Created: 2015-12-18T18:01:40.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-18T19:12:56.000Z (almost 9 years ago)
- Last Synced: 2024-08-08T19:43:07.653Z (3 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Infix Arithmetic Expression Problem
I have gotten this question in an interview before and wanted to work through
it to get it right. Let me know if you find a better way!## Question
Write a function that given an infix arithmetic expression, outputs a prefix version (the output order is unimportant as long as it evaluates to the same result).
For example:
3 becomes 3
1 + 2 becomes (+ 1 2)
3 + 1 - 2 becomes (- (+ 3 1) 2)1 + 2 * 5 becomes (+ 1 (* 2 5))
2 + 3 * 4 / 5 becomes (+ 2 (/ (* 3 4) 5)
1 + 3 * 4 - 8 becomes (- (+ 1 (* 3 4) 8)Quick notes:
- Parentheses in the output are not important - they are only in the examples for readability.
- You can assume input and output are given as arrays of strings, e.g. [“3”, “+”, “1”, … etc.]
- You can assume there are convenience functions such as isOperator.## To Test
```bash
npm install infix-to-prefix
npm test
```[Here is the actual code](function.js)
[Here are the tests](test/function_tests.js)