11
$\begingroup$

While coding in Python, I realized I couldn't remember which is evaluated first:

and (the logical conjunctive operator),
or (the logical disjunctive operator).

Of course, I could've used parentheses (or looked it up), but I wanted to try to find out for myself. I tried endless combinations of False, True, and, and or but could not figure it out.

Given a language with unknown operator precedence, where operators of equal precedence evaluate left-to-right, find a way to conclusively determine whether the logical conjunction or the logical disjunction is evaluated first.

You can only use False, True, and, and or in your answer.

Note: You may assume that or and and definitely have different precedence.

$\endgroup$
7
  • $\begingroup$ Are we assuming operator precedence either (1) evaluates all and left to right and then all or left to right, or (2) evaluates all or left to right and then all and left to right? $\endgroup$
    – noedne
    Commented Apr 30, 2018 at 1:35
  • $\begingroup$ @noedne Yes. Otherwise they'd have the same precedence $\endgroup$
    – somebody
    Commented Apr 30, 2018 at 1:37
  • 1
    $\begingroup$ @noedne edited. $\endgroup$
    – AAM111
    Commented Apr 30, 2018 at 2:34
  • 1
    $\begingroup$ Nice edit. Do we also assume that and and or have different precedence? $\endgroup$
    – noedne
    Commented Apr 30, 2018 at 2:42
  • 1
    $\begingroup$ Yes, as stated (slightly unclearly) in the question. Let me clarify that. $\endgroup$
    – AAM111
    Commented Apr 30, 2018 at 2:51

1 Answer 1

18
$\begingroup$

True or True and False

Giving True means 'and' then 'or'. Giving False means 'or' then 'and'.

Bonus:

False and True or True

Also use this to test if the precedence is same. If both tests are resulting different value (first test gives False and second test gives True) then the precedence is same.

$\endgroup$
2
  • $\begingroup$ Wow, how did I not think of that… facepalm Good job! $\endgroup$
    – AAM111
    Commented Apr 30, 2018 at 1:42
  • 5
    $\begingroup$ The middle value is actually irrelevant. It could be either true or false and the test would still work the same. $\endgroup$
    – Kruga
    Commented Apr 30, 2018 at 8:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.