Species year Logical logic: include brackets?

What is more correct (logically)? Specific for a leap year, not at all.

  • With parentheses

    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
    
  • Without

    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
    

Additional Information

The brackets change the order in which the logical values ​​are calculated ( andgoes to orwithout brackets).

Given that all large numbers are divided by smaller numbers in this task, it returns the correct result anyway, but I'm still interested.

Watch the actions of parentheses:

  • False and True or True
    #True
    
    False and (True or True)
    #False
    
  • False and False or True
    #True
    
    False and (False or True)
    #False
    

, 4 ( bool), True ( , )! 4 a , ? - ? - / ?

+4
7

:


, , 3, → 2 :

  • 4.
  • (2) 100, (3), 400.

.

return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
       ^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            (1)                          (2)

, , :

  • 4, 100;
  • 400.

, .


mrdomoboto, 100/400 !:

4, 100 - , 400 - , (. ). , 4, . - , False and bool False.

JBallin

  • False and True or True
    #True
    
    False and (True or True)
    #False
    
  • False and False or True
    #True
    
    False and (False or True)
    #False
    

:

, 4, , 100 400, :

return y % 4 == 0 and not (y % 100 == 0 and y % 400 != 0)

JBallin De Morgan Laws:

not(a and b) = (not a or not b)

:

#convert using "DML"
return y % 4 == 0 and (not y % 100 == 0 or not y % 400 != 0)
#remove "not"s by switching "==" and "!="
return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
+1

, . and or, :

a and b or c

:

(a and b) or c

BOTH a b , OR, c , True.

:

a and (b or c)

True, a , b OR c .


"", , , " " . parens, , . :

if (a and b) or c:

,

if a and b or c:

( ), :

if some_long_identifier and some_other_long_identifier or \
   some_third_long_identifier_on_another_line:

Python PEP8. PEP8 , (: parens, ), .


:

  • 4, 2....
  • 100, 3....
  • 400, 4....
  • - ( 366 ).
  • ( 365 ).

: , 4, , 100 400, :

return y % 4 == 0 and not (y % 100 == 0 and y % 400 != 0)
+5

. :

  • 4.
  • 100, 400.

.

return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
       ^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            (1)                          (2)

, , :

  • 4, 100;
  • 400.

, .

+3

" " ?

, " "; ? Parenthesis ' boolean . .

>>> (True or True) and False  # or expression evaluates first.
False
>>> True or True and False  # and evaluates first.
True

, :

  • - , (, 2012, 2016 ..).

  • , 100, (, 2100, 2200 ..)

  • , 400, (, 2000, 2400)

, , or . , and.

+2

" " ? ( , )

% 4 == 0 (% 100!= 0 % 400 == 0)

% 4 == 0 % 100!= 0 % 400 == 0

" ". , .

" " - , , .

, ,

% 4 == 0 % 100!= 0 % 400 == 0

, , , , , , .

, :

return (% 4 == 0 % 100!= 0) % 400 == 0

+1

, , , 400, , 100, , 4. , ( ) . c, , . , .

, , . , " " " ".

return (year%400 == 0) or (year%100 != 0 and year%4 == 0)

bool IsLeap = false;
if (year%4 == 0) IsLeap = true;
if (year%100 == 0) IsLeap = false;
if (year%400 == 0) IsLeap = true;

return IsLeap;

, , , , .

+1

: .

( )...

year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) :

A and (B or C)

A , B C and , true.

and or " " , Python, (. Https://docs.python.org/2/library/stdtypes.html#boolean-operations )

  • and , false ( true)
  • or , true ( false)

:

A and (B or C)

  • A false, (B or C) , .
  • A true, B , C , B .

:

A and B or C

  • A false, B , C ( ).
  • A true, B . B false, C .

: C ( year % 400) , A ( year % 4). 75% , A, .

:

: ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0))

( ) (!) .

: fooobar.com/questions/34564/...

0

Source: https://habr.com/ru/post/1651311/