Skip to main content
I can't count in RPN...
Source Link
TorbenPutkonen
  • 9.3k
  • 21
  • 29

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you get

2 3 4 + * = 2014
2 3 4 * + = 1014
2 4 3 + * = 1814
2 4 3 * + = 1114
...

Ok, those were pretty badly chosen operators and operands, but you get the idea. :D

The number of expressions you generate will be x! * (x-1)! where x is the number of operands. So for 6 operands you will create 86400 expressions, 3628800 for seven and 203212800 for eight. The factorial really isn't your friend if you want to store data in memory...

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you get

2 3 4 + * = 20
2 3 4 * + = 10
2 4 3 + * = 18
2 4 3 * + = 11
...

The number of expressions you generate will be x! * (x-1)! where x is the number of operands. So for 6 operands you will create 86400 expressions, 3628800 for seven and 203212800 for eight. The factorial really isn't your friend if you want to store data in memory...

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you get

2 3 4 + * = 14
2 3 4 * + = 14
2 4 3 + * = 14
2 4 3 * + = 14
...

Ok, those were pretty badly chosen operators and operands, but you get the idea. :D

The number of expressions you generate will be x! * (x-1)! where x is the number of operands. So for 6 operands you will create 86400 expressions, 3628800 for seven and 203212800 for eight. The factorial really isn't your friend if you want to store data in memory...

added 326 characters in body
Source Link
TorbenPutkonen
  • 9.3k
  • 21
  • 29

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you give youget

2 3 4 + * = 20
2 3 4 * + = 10
2 4 3 + * = 18
2 4 3 * + = 11
...

The number of expressions you generate will be x! * (x-1)! where x is the number of operands. So for 6 operands you will create 86400 expressions, 3628800 for seven and 203212800 for eight. The factorial really isn't your friend if you want to store data in memory...

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you give you

2 3 4 + * = 20
2 3 4 * + = 10
2 4 3 + * = 18
2 4 3 * + = 11
...

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you get

2 3 4 + * = 20
2 3 4 * + = 10
2 4 3 + * = 18
2 4 3 * + = 11
...

The number of expressions you generate will be x! * (x-1)! where x is the number of operands. So for 6 operands you will create 86400 expressions, 3628800 for seven and 203212800 for eight. The factorial really isn't your friend if you want to store data in memory...

Source Link
TorbenPutkonen
  • 9.3k
  • 21
  • 29

Using infix expressions makes the code very complicated. If you were using postfix (commonly known as RPN) you could calculate each permutation for the operands and for each permutation, calculate every permutation of the operators and simply append them. No need to worry about operator precedence or parenthesis.

For example, operands [ 2, 3, 4 ] and operators [ +, * ] you give you

2 3 4 + * = 20
2 3 4 * + = 10
2 4 3 + * = 18
2 4 3 * + = 11
...