2

Bit of a long read, It's my first question here :)

So, I want a function or the logic, whereby the code is able to validate an expression whether it is Infix, Postfix, or Prefix Accurately. So by accurate I mean it should check all conditions whether the expression itself is valid, what the expression is (In, Pre or Post), or tell us if it's invalid.

I googled this question but all I found was check the first three digits of the expression. But this logic doesn't hold true when the rest of the expression is wrong and I want a far more superior validator.

So here's my take and I want to find any mistakes/edge cases in these.

1] Check the first 3 digits for infix and postfix and the last 3 for postfix and they should be in their respective order. (e.g AB* for postfix and A*B for infix)

2] Every valid expression has N operands and N-1 operators (A+B-C has 3 operands and 2 operators) so check that too, so we can't have tailing operators (something invalid like AB+C-+--)

3] Check the full validity using a stack (if (operand) push it, if (operator), pop twice, valid if the stack is empty at the end). For Infix we use two stacks one for operand and one for the operator and then validate.

4] If all three are correct, then return with an appropriate answer. (can make 3 separate functions if need be)

Sorry for the long read. Is my logic perfect? Could there be any edge cases or mistakes in this one? Thanks!

SaurabhV
  • 21
  • 1
  • You use the term _digits_ inappropriately; you may mean e. g. symbols. – Armali Jul 16 '21 at 21:35
  • _Check the first 3_ doesn't account for expressions with N = 1. – Armali Jul 16 '21 at 21:36
  • 2
    The best thing you can do is to define a formal grammar. – Eugene Sh. Jul 16 '21 at 21:37
  • *Checking the first 3* doesn't account for numbers that more than 1 digit in length or that are inconsistent in length. For example `+ 1000 200`. Also, interestingly, the `+` and '-` are valid characters for a number, indicating the signage of the number. – Thomas Matthews Jul 16 '21 at 21:44
  • @SaurabhV - You restrict your consideration to operators with two operands - that's okay, but should perhaps be made clear from the start. – Armali Jul 16 '21 at 21:47
  • _3]_ is incompletely described for infix and prefix, so it cannot be judged if there are mistakes. – Armali Jul 16 '21 at 21:51
  • 1
    just parse it as infix, prefix and postfix and see any the expression is valid in any of those functions. – bakaDev Jul 16 '21 at 23:16
  • To find mistakes, you have to do the entire calculation and check if there is anything left. (Because lets say, the number of +, - remains ok but they are put in the wrong place. This for example: a+b c-) – Daoist Paul Jul 17 '21 at 01:57
  • 1
    For the expression `-2-3`, is it infix `(-2) - 3 == -5`, or prefix `- (2, -3) == 5`, or 2 negative numbers with no operators (invalid)? – Brendan Jul 17 '21 at 04:27
  • 1
    @Armali Yes you're right! I meant symbols not digits. Also you're right that it wouldn't work when expression has less than 3 symbols, duly noted. About being incompletely described, I had assumed that the loop would start from beginning for post fix and from the end for prefix – SaurabhV Jul 18 '21 at 00:32
  • The infix case is still incompletely described, but one can imagine what is meant. - If you simply drop step 1], N = 1 is also handled. In fact if done right, step 3] alone suffices. – Armali Jul 18 '21 at 07:19

0 Answers0