Examples correct?

  • 0

    I believe that examples 2 and 3 had typos, and I can't figure out the second column of example 3.

    tl;dr - I think the second columns were included by mistake.

    Example 2

    Input: "F?1:T?4:5"
    Output: "4"
    Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:
                 "(F ? 1 : (T ? 4 : 5))"                   "(F ? 1 : (T ? 4 : 5))"
              -> "(F ? 1 : 4)"                 or       -> "(T ? 4 : 5)"
              -> "4"                                    -> "4"

    I think the top line of column 2 should read:

    "((F ? 1 : T) ? 4 : 5)"

    in order to get the next line in that column.

    Example 3

    Input: "T?T?F:5:3"
    Output: "F"
    Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:
                 "(T ? (T ? F : 5) : 3)"                   "(T ? (T ? F : 5) : 3)"
              -> "(T ? F : 3)"                 or       -> "(T ? F : 5)"
              -> "F"                                    -> "F"

    Again, I think that the top line of column 2 is a typo (it repeats the top line of column 1). Regardless, however, I don't see how it's possible to get the next line of column 2 at all. Does anyone see it?

    In any case, I thought that "conditional expressions group right-to-left" was to avoid ambiguity. If, on the other hand, that rule still allows for some ambiguity (I don't think it does), can one prove that the answer is always the same?

    ...or maybe I'm missing something obvious!

  • 2

    I think it won't be a problem here. "The conditional expressions group right-to-left (as usual in most languages)." It means just group right-to-left, but not execute in any certain order.

    For example 2, if the inner expression first to be executed, then next step is (F ? 1 : 4), otherwise (F ? 1 : ([InnerExpression])) results in [InnerExpression], which is (T ? 4 : 5). But it can't be ((F ? 1 : T) ? 4 : 5)) => (T?4:5) => 4.

  • 0

    To expand on @Undo: In each case, you evaluate two conditions. It doesn't matter which one you evaluate first, and the two columns merely show both evaluation orders to avoid suggesting a preference. The left column evaluates the inner condition first, the right column evaluates the outer condition first. ((F ? 1 : T) ? 4 : 5) is wrong, that's not how F ? 1 : T ? 4 : 5 is supposed to be grouped. It's also not how any language I know does it, except for PHP, which gets properly ridiculed for it and whose own documentation says "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious".

  • 0

    Actually, if one of the columns surprises/confuses you, it should be the first column, not the second. Because the second column, evaluating outside-first, is how ternaries are evaluated in real life by real programming languages. That's what people should be familiar with. If you have True ? X : Y, then the whole Y (which could be a huge complicated expression), doesn't need to be evaluated at all, and (at least in the languages I know), isn't.

  • 0

    Ahhh, I see! I was conflating grouping with order of execution, and desperately trying to find a grouping that would get those examples to make sense to me. It's totally clear now, thank you both!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.