Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
' | ' Operator and 'OR'
#8
Thank you all for replying guys. To be more precise I have to say that version with '==0' (after first expression) and without it both works the same.
is_divisible = lambda n,x,y : n % x | n % y == 0

Time: 614ms Passed: 50 Failed: 0
Test Results:
 Basic tests
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
Test Passed
 Random tests
 Testing for is_divisible(85, 7, 4)
 Testing for is_divisible(2521, 7, 18)
 Testing for is_divisible(1486, 9, 11)
 Testing for is_divisible(864, 9, 16)
 Testing for is_divisible(21, 7, 1)
 Testing for is_divisible(321, 1, 20)
 Testing for is_divisible(91, 6, 15)
 Testing for is_divisible(280, 8, 7)
 Testing for is_divisible(432, 3, 9)
 Testing for is_divisible(1361, 10, 8)
 Testing for is_divisible(2592, 9, 16)
 Testing for is_divisible(433, 9, 6)
 Testing for is_divisible(1215, 9, 9)
 Testing for is_divisible(96, 1, 8)
 Testing for is_divisible(560, 7, 20)
 Testing for is_divisible(1801, 10, 20)
 Testing for is_divisible(525, 5, 7)
 Testing for is_divisible(55, 5, 11)
 Testing for is_divisible(75, 5, 5)
Test Passed
 Testing for is_divisible(448, 8, 7)
 Testing for is_divisible(80, 8, 1)
 Testing for is_divisible(126, 9, 7)
 Testing for is_divisible(162, 9, 18)
Test Passed
 Testing for is_divisible(72, 4, 18)
 Testing for is_divisible(601, 10, 4)
 Testing for is_divisible(12, 2, 1)
 Testing for is_divisible(120, 10, 1)
 Testing for is_divisible(48, 3, 16)
 Testing for is_divisible(181, 3, 12)
 Testing for is_divisible(409, 8, 3)
 Testing for is_divisible(200, 5, 8)
 Testing for is_divisible(840, 3, 20)
 Testing for is_divisible(337, 6, 4)
Test Passed
 Testing for is_divisible(961, 4, 20)
 Testing for is_divisible(140, 7, 5)
 Testing for is_divisible(545, 2, 17)
 Testing for is_divisible(57, 4, 2)
 Testing for is_divisible(649, 9, 4)
 Testing for is_divisible(1065, 8, 19)
 Testing for is_divisible(32, 8, 4)
You have passed all of the tests! :)
Exactly the same results with '==0'.
But there is a difference between '|' and 'or':

is_divisible = lambda n,x,y : n % x ==0 or n % y == 0
#without ==0 got same results.
Time: 637ms Passed: 44 Failed: 6 Exit Code: 1
Test Results:
 Basic tests
True should equal False
Test Passed
True should equal False
Test Passed
Test Passed
True should equal False
Test Passed
Test Passed
Test Passed
Test Passed
Basic tests are very simple and looks like this, but version with 'or' wont even pass these:

Test.assert_equals(is_divisible(3,3,4),False)
Test.assert_equals(is_divisible(12,3,4),True)
Test.assert_equals(is_divisible(8,3,4),False)
Test.assert_equals(is_divisible(48,3,4),True)
Its pretty tricky to get whats going on here and on which part pythons 'or' is different from bitwise '|' for booleans.

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import dis
>>> def div(n,x,y):
	return n % x ==0 or n % y == 0

>>> div(151,1,15)
True #Version with 'or' got the wrong answer.
>>> dis.dis('div(151,1,15)')
	
  1           0 LOAD_NAME                0 (div)
              2 LOAD_CONST               0 (151)
              4 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 (15)
              8 CALL_FUNCTION            3
             10 RETURN_VALUE

>>> def div(n,x,y):
	return n % x | n % y == 0
>>> div(151,1,15)
False # Bitwise got it right.

>>> dis.dis('div(151,1,15)')
	
  1           0 LOAD_NAME                0 (div)
              2 LOAD_CONST               0 (151)
              4 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 (15)
              8 CALL_FUNCTION            3
             10 RETURN_VALUE
#No differences in disassembly.
At random tests 'or' fails on every case with '1' in it. On basic ones it fails about half of it:

>>> div(8,3,4)
	
False # Bitwise is correct.
>>> def div(n,x,y):
	return n % x == 0 or n % y == 0

	
>>> div(8,3,4)
	
True # 'Or' is wrong again.
>>> 
So basically pythons 'or' does what its supposed to do and gets one that is True, but 'bitwise OR' works completely different, it retuns True only if both cases are True and I dont know why because from what I've learned it should change '0' for '1' if there is '1' in same point of any of the expressions, am I missing something? I hope at least you understand me better now!
Sorry for all the chaos.
Reply


Messages In This Thread
' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 01:10 AM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 02:24 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 04:47 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 06:28 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 06:40 PM
RE: ' | ' Operator and 'OR' - by Gribouillis - Jan-10-2019, 06:54 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 07:08 PM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 11:08 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-11-2019, 01:23 AM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-11-2019, 05:51 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-11-2019, 09:32 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020