Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
' | ' Operator and 'OR'
#11
(Jan-11-2019, 05:51 AM)nilamo Wrote: Those are not the same thing. If you add the "==0" check to the bitwise operation, you'd also have failures. ie: this should fail:
is_divisible = lambda n,x,y: n%x==0 | n%y==0

No sir, it works perfectly fine:
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(2584, 8, 19)
 Testing for is_divisible(104, 4, 13)
 Testing for is_divisible(900, 6, 15)
 Testing for is_divisible(1144, 8, 13)
 Testing for is_divisible(648, 9, 6)
 Testing for is_divisible(487, 3, 9)
 Testing for is_divisible(127, 6, 3)
 Testing for is_divisible(267, 7, 2)
 Testing for is_divisible(69, 4, 1)
 Testing for is_divisible(817, 6, 8)
 Testing for is_divisible(481, 6, 4)
 Testing for is_divisible(480, 4, 8)
 Testing for is_divisible(784, 7, 14)
 Testing for is_divisible(811, 9, 15)
 Testing for is_divisible(108, 9, 6)
 Testing for is_divisible(1764, 9, 14)
 Testing for is_divisible(676, 5, 9)
 Testing for is_divisible(205, 4, 3)
 Testing for is_divisible(19, 6, 3)
 Testing for is_divisible(253, 7, 3)
 Testing for is_divisible(126, 3, 7)
 Testing for is_divisible(160, 10, 4)
 Testing for is_divisible(39, 2, 19)
 Testing for is_divisible(1276, 5, 15)
 Testing for is_divisible(756, 6, 18)
 Testing for is_divisible(251, 10, 5)
 Testing for is_divisible(2908, 9, 19)
 Testing for is_divisible(2251, 10, 15)
 Testing for is_divisible(749, 4, 17)
 Testing for is_divisible(991, 6, 11)
 Testing for is_divisible(252, 6, 3)
 Testing for is_divisible(90, 2, 15)
 Testing for is_divisible(1041, 4, 13)
 Testing for is_divisible(2080, 8, 20)
 Testing for is_divisible(1360, 5, 17)
 Testing for is_divisible(504, 6, 14)
 Testing for is_divisible(911, 10, 13)
 Testing for is_divisible(577, 8, 18)
 Testing for is_divisible(1600, 5, 20)
 Testing for is_divisible(1153, 6, 12)
You have passed all of the tests! :)
Quote:Likewise, if you use or the same as you're currently using the bitwise operator, it would pass. ie: this should succeed:
is_divisible = lambda n,x,y: n%x or n%y == 0
Nope, its far away from working flawless as bitwise version:
Time: 593ms Passed: 34 Failed: 16 Exit Code: 1
Test Results:
 Basic tests
Test Passed
Test Passed
2 should equal False
Test Passed
Test Passed
Test Passed
Test Passed
1 should equal False
Test Passed
Test Passed
 Random tests
 Testing for is_divisible(2280, 8, 15)
 Testing for is_divisible(2080, 10, 13)
 Testing for is_divisible(480, 8, 10)
 Testing for is_divisible(57, 8, 1)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(1351, 5, 15)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(5, 5, 1)
 Testing for is_divisible(2100, 10, 14)
 Testing for is_divisible(78, 7, 1)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(315, 3, 7)
 Testing for is_divisible(1360, 8, 17)
 Testing for is_divisible(126, 9, 1)
 Testing for is_divisible(92, 7, 1)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(2052, 9, 19)
 Testing for is_divisible(280, 2, 7)
 Testing for is_divisible(26, 5, 5)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(970, 3, 17)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(78, 1, 6)
 Testing for is_divisible(1680, 7, 20)
 Testing for is_divisible(180, 4, 9)
 Testing for is_divisible(336, 8, 6)
 Testing for is_divisible(1921, 6, 16)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(41, 1, 4)
 Testing for is_divisible(588, 7, 6)
 Testing for is_divisible(721, 4, 12)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(420, 6, 7)
 Testing for is_divisible(468, 3, 12)
 Testing for is_divisible(480, 5, 8)
 Testing for is_divisible(1153, 8, 9)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(55, 6, 1)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(1248, 6, 16)
 Testing for is_divisible(196, 7, 4)
 Testing for is_divisible(252, 1, 18)
 Testing for is_divisible(393, 8, 7)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(1801, 10, 20)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(510, 10, 3)
 Testing for is_divisible(54, 6, 1)
 Testing for is_divisible(421, 5, 12)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(267, 7, 2)
It should work for random tests too: 1 should equal False
 Testing for is_divisible(810, 5, 18)
 Testing for is_divisible(221, 1, 17)
So let me list some working expressions to make it more simple:
is_divisible = lambda n,x,y : n % x | n % y == 0
is_divisible = lambda n,x,y : n % x ==0 | n % y == 0
is_divisible = lambda n,x,y : n % x == 0 and n % y == 0
is_divisible = lambda n,x,y : not (n%x or n%y) #Only one without bitwise operation that works with both 'or' and binary or.
Expressions that doesnt work :
is_divisible = lambda n,x,y : n % x ==0 or n % y == 0 #Or is not equal to the 'binary or' even for booleans.
is_divisible = lambda n,x,y : n % x and n % y == 0 #Works if you change 'and' for binary or.
is_divisible = lambda n,x,y: n%x or n%y == 0 #Doesnt works because pythons 'or' works just as or, only binary or is more like 'and', and I dont know precisely why is that, but stullis pointed out very interesting thing with priority of equality vs bitwise operations.
Im not competent enough to analyze all the odd things here, but I think I understand this better now. Thanks mighty folks, take care!
Reply


Forum Jump:

User Panel Messages

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