Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)