+ 1

What should be the value of "0 == 0 == 0" ?

What about 1 == 0 == 0, 0 == 1 == 0 and 0 == 0 == 1 ? Then what about 2 == 2 == 2 ?

14th Mar 2019, 6:40 PM
Kristian Benoit
Kristian Benoit - avatar
15 Respuestas
+ 9
1 and 5 will be True and others will be False in Python because comparisons can be chained in Python https://docs.python.org/3/reference/expressions.html#comparisons
14th Mar 2019, 8:00 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
1) "0 == 0 == 0" This will evaluate to "false" as "0 == 0" will be "true" and then true as a number will be converted to "1" so it will be "1 == 0" which is false. 2) "1 == 0 == 0" This will evaluate to "true" since "1 == 0" will be "false" and then "false" as a number will be converted to "0" so it will be "0 == 0" which is true. 3) "0 == 1 == 0" This will evaluate to "true" since "0 == 1" will be "false" and then "false" as a number will be converted to "0" so it will be "0 == 0" which is true. 4) "0 == 0 == 1" This will evaluate to "true" since "0 == 0" will be "true" and then "true" as a number will be converted to "1" so it will be "1 == 1" which is true. 5) "2 == 2 == 2" This will evaluate to "false" since "2 == 2" will be "true" and then "true" as a number will be converted to "1" so it will be "1 == 2" which is false. Edit: As Diego pointed out this answer is specific to JavaScript and results may vary for other languages (e.g. Python)
14th Mar 2019, 7:11 PM
LynTon
LynTon - avatar
+ 6
I prefer it if code explans itself. In Python it seems like a == b == c is not evaluated ltr or rtl but the same as all(a,b,c) Edit: as Diego pointed out that is not true for (0,0,0). Edit2: it seems to be interpreted the same as all(i==p for p in (i,j,k)) https://code.sololearn.com/c3Ipf3qh78vG/?ref=app https://code.sololearn.com/ceaMrz6WjA34/?ref=app
15th Mar 2019, 2:32 AM
Louis
Louis - avatar
+ 5
Diego Ok so, I didn't realize that using different languages would leave different results for this question. I typically use JavaScript as my primary programming language and since it was tagged I immediately went with that and with it my answer would be correct. https://code.sololearn.com/WV3ailR0yn9r/?ref=app But I see what you mean, thanks for pointing that out
15th Mar 2019, 12:46 AM
LynTon
LynTon - avatar
+ 4
Tibor Santa I have a solution that caters for all chained cases. See https://code.sololearn.com/ceaMrz6WjA34/?ref=app
16th Mar 2019, 8:57 AM
Louis
Louis - avatar
+ 3
Louis If we use only the == operator then yes, the result in python is matching your all() formula. But when generalizing the case for any comparison operaror, each comparison is executed only in relation to the preceding operand. Example: a < b <= c == d is true if: a < b and b <= c and c == d
15th Mar 2019, 5:36 AM
Tibor Santa
Tibor Santa - avatar
+ 2
good question i saw this in one of the quizes and was silently wondering how they got their answer, so thanks for your answers
14th Mar 2019, 8:16 PM
Buhle 💃💃💃
Buhle 💃💃💃 - avatar
+ 2
== is an operator, program reads operators first by following the operator precedence and then following the order of the operators. Comparison operators always return a boolean, and booleans can be compared with other numeric datatypes. (Boolean can be thought as a numeric datatype with 2 possible values.)
14th Mar 2019, 9:23 PM
Seb TheS
Seb TheS - avatar
+ 2
[Python] LynTon From Mert's answer: "Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once". Therefore, 1 == 0 == 0 is equal to (1 == 0 and 0 == 0), which is equal to (False and True), which evaluates to False. Same goes for all of the other cases.
14th Mar 2019, 10:12 PM
Diego
Diego - avatar
+ 2
LynTon Interesting to see how diferrente languages handle the same problem/logic. Kristian Benoit This just goes to show you how important it is to specify the desired programming language in the question/tags as it can lead to some confusion.
15th Mar 2019, 12:54 AM
Diego
Diego - avatar
+ 2
Louis 0 == 0 == 0 returns True. all(0,0,0) returns False.
15th Mar 2019, 2:38 AM
Diego
Diego - avatar
+ 2
Diego, I mostly used Python, but faced the problem using JavaScript. I asked the question to create a discussion and let other devs realize the ambiguity. So it was intentional to tag all languages.
15th Mar 2019, 4:20 AM
Kristian Benoit
Kristian Benoit - avatar
+ 2
Seb TheS Yes, they are, but I was talking more about chainging than the operators themselves. For example, 1 == 0 == 0 evaluates to False in Python but in JavaScript it evaluates to True.
15th Mar 2019, 7:22 AM
Diego
Diego - avatar
+ 2
Louis awesome. :)
16th Mar 2019, 10:43 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Diego aren't comparison operators quite same in most programming languages?
15th Mar 2019, 6:07 AM
Seb TheS
Seb TheS - avatar