def my_function(x,y,z):
out = True
if(x < y) | (x > z):
out = False
return out
Can you help me understand what this is doing? Is it, "out is True, but if x is less than y or greater than z: out is False"? I am unsure about the | operator.
TL;DR, someone thought they were clever.
This code takes advantage of some implementation details about booleans and then does some binary integer operations.
To understand it we need to cover a few things:
the > and < operators in a comparison will give back boolean True or False.
Boolean True and False are actually subclasses of int and function as 1 and 0 respectively in operations that expect an integer (try running True + 1 in your REPL).
The | operator is bitwise OR (as opposed to logical OR which is just or) which works on integers.
And now we can understand the code: the <> comparisons gives us a bool that gets used as an integer 1 or 0 and ORed bitwise with the result of the other comparison.
An even cleverer person would note that bitwise OR with 1's and 0's would only be 0 if both were 0, and just check for that condition:
1 | 1 # 1
1 | 0 # 1
0 | 1 # 1
0 | 0 # 0
This code is needlessly obfuscated: there's no reason to treat the True/False as 1/0 just to do bit manipulation when regular boolean logic would work. This will work identically:
def my_function(x, y, z):
return y <= x <= z
Best way to wrap your head around code you are not sure about is to test it:
testValues = [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
for x, y, z in testValues:
print("The values are: x = %d, y = %d, z = %d" % (x, y, z))
print("out is:", my_function(x, y, z))
def my_function(x,y,z):
global out
out = True
if(x < y) | (x > z):
out = False
return out
x = 6
y = 2
z = 5
my_function(x,y,z)
The above code outputs True/False based on the values of x,y,z. If x is smaller than y OR if x is greater than z it will change the value of 'out' to False.You can further customize the code to do stuff with the output. Like so:
if out == True:
//some stuff to execute when true
else:
//some stuff to execute when false
Related
In numbers like 16, performing bitwise AND on all numbers from 1 to 15 gives result 0. I want to find out the first number that doesn't give 0 when bitwise AND is performed on it.
TLDR : Find smallest x such that x AND y ! = 0
y is given
You can do brute force approach:
while True:
y=int(input('y='))
x=1
while True:
if(x & y !=0):
print(x)
break
x*=2
The solution is to find the first bit from right, which is 1.
For example,
y = 16 = 0b10000
^
y = 12 = 0b1100
^
def get_x(y):
i = 0
while True:
if (y >> i) & 1 == 1:
break
i += 1
return 1 << i
This seems cleverer.
def get_x(y):
return y & (-y)
Find the lowest set bit
Maybe it's too trivial question but I can't find answer if there is a some more elegant way to cast int value to 0 or 1 without using condition or type casting?
Now I have only following variants and both are ugly:
x = 5
a = some_int_value * (1 if x > 0 else 0)
b = some_int_value * int(bool(x))
ADDED
x is a non-negative value.
To paraphrase your condition, x can be 0 or a value larger than 0. If x > 0, you want to use the value some_int_value, otherwise you want 0 (which is identical to x). Then do:
c = x and some_int_value
If x is 0, i.e. falsey, the and expression returns x. If x is truthy (non-zero), it returns some_int_value.
Arguably even more comprehensible would be:
d = some_int_value if x > 0 else 0
Or:
e = 0
if x > 0:
e = some_int_value
Another way without functions, casting, or conditionals, but with comparisons:
d = some_int_value * (x > 0)
However, in terms of readability, a ternary ... if ... else ... should probably be preferred.
If you want a purely mathematical way, you can use exponentiation with 1 - 0x, since 0**x is 1 only if x == 0 and 0 otherwise. But whether that is in any way clear is another question. (Also note that this gives a division-by-zero for x < 0, but you said that you don't have those.)
>>> x = 5
>>> 1 - 0**x
1
>>> x = 0
>>> 1 - 0**x
0
What does this y & 3 do exactly and how does this if..if y & 3..else statement works ? Please anybody could explain ? Taken from https://www.hackerrank.com/challenges/day-of-the-programmer/leaderboard
y = int(input())
if y < 1918:
ftext = "13.09.{}" if y & 3 else "12.09.{}"
elif y > 1918:
ftext = "13.09.{}" if (y & 3) or (y % 400 and not y % 100) else "12.09.{}"
else:
ftext = "26.09.{}"
print(y & 3)
print(ftext.format(y))
& operator does a bitwise AND between the operands.
y&3 masks the bits of y with the bits of 3.
Thus if y = 11110111 and 3 = 00000011
y&3 will evaluate to 00000011
"13.09.{}" if y & 3 else "12.09.{}" checks the condition whether the last 2 bits of y are not both zero. if the last 2 bits of y are zero, the condition will become false.
from math import *
def prime():
a = 0
b = 0
x = 2*a+1
y = b
for a in range (1,5000) and b in range (0,5000) and y<x :
ctr = 0
if (x % y == 0):
ctr += 1
return [None]
else:
primes = (x)
ctr+= 1
return [None]
print (primes[999]);
I have a problem I need to solve but when it gets to the modulo ( %) sign it says TypeError: 'bool' object is not iterable"
Instead of comparing the values, you are assigning 0 to x % y, which is not possible. You can fix it like this
if (x % y == 0):
After fixing that,
for a in range (1,5000) and b in range (0,5000) and y<x :
this line will not work, you have to split the loops like this
for a in range (1,5000):
for b in range (0,5000):
...
...
The modulo sign is fine, note the ^ in the error message.
File "so-modulo-test.py", line 11
if (x % y = 0):
^
SyntaxError: invalid syntax
= indicates an assignment statement. You want ==, the equal comparison operator, as in
if x % y == 0:
Additionally, and is the logical AND, and not to be confused with how a human would talk. Your loop should look like:
for a in range (1,5000):
for b in range (0, x):
...
I want to encode If-the-else in Z3 python, but cannot find any docs or sample on how to do that.
I have a sample code like below.
F = True
tmp = BitVec('tmp', 1)
tmp1 = BitVec('tmp1', 8)
Now how can I encode this condition into F:
if tmp == 1, then tmp1 == 100. otherwise, tmp1 == 0
Thanks so much.
You'll need Z3's If function:
def z3py.If ( a,
b,
c,
ctx = None
)
Create a Z3 if-then-else expression.
>>> x = Int('x')
>>> y = Int('y')
>>> max = If(x > y, x, y)
>>> max
If(x > y, x, y)
>>> simplify(max)
If(x <= y, y, x)
(from here)
You can use If for this. If takes three arguments: the condition, an expression that should be true if the condition is true and an expression that should be true if the condition is false. So to express your logic, you'd write:
If(tmp==1, tmp1==100, tmp1==0)