Are there sideeffects in python using `if a == b == c: pass;`? - python

if a == b == c:
# do something
Let's assume a, b, c are string variables. Are there any possible side effects if I use the snippet above to execute # do something if and only if all three strings are equal?
I am asking because I have to check three variables against each other and I get many cases:
if a == b == c:
# do something
elif a == b != c:
# do something
elif a != b == c.
# do something
etc...
Perhaps there is a better way to code this?

There should be no side effects until you use it in a such way.
But take care about things like:
if (a == b) == c:
since it will break chaining and you will be comparing True or False and c value).

From the documentation:
Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
There should be no side effects.

s = set([a, b, c])
if len(s) == 1:
print 'All equal'
elif len(s) == 3:
print 'All different'
else:
l = list(s)
print '%s and %s are different' % (l[0], l[1])

is there any comment on x!=y!=z ?
i could use the stupid way to get a correct answer.
def aligndigits(list):
return ((x, y , z ) for x in list for y in list for z in list if x != y and y != z and x != z )

Related

What does [b, c][a < b < c]+'000'[a < c:] expression do?

This is the code about to find out dice gamble's prize amount:
a, b, c = map(int, input().split())
if a == b and b == c: #same all of dice numbers
print(10000 + (a * 1000))
elif a == b or b == c: #same two of dice numbers
print(1000 + (b * 100))
elif a == c: #same two of dice numbers
print(1000 + (a * 100))
else: #no same number
print(max(a, b, c)*100)
This is equivalent:
*_, a, b, c=sorted(input())
print(['1'+b, c][a < b < c]+'000'[a < c:])
But i can't understanding about what does
['1'+b, c][a < b < c]
and
'000'[a < c:]
do.
So, I had tried to find out the meaning of
`['1'+b, c][a < b < c]`
I found this is similar with
`c if a<b<c else '1'+b`
but i can't be sure about that.
anyway, about
`'000'[a < c:]`
I tried input a=c to
`print('000'[a < c:])`
It shows 000.
I tried else that input a<c, it shows 00
Anyone can tell me about this expression?
The original is unnecessarily cryptic. It uses the fact that:
int(False) == 0
int(True) == 1
For instance,
'000'[False:] == '000'[0:] == '000'
'000'[True:] == '000'[1:] == '00'
Similarly,
['1' + b, c][False] == ['1' + b, c][0] == '1' + b
['1' + b, c][True] == ['1' + b, c][1] == c
Here's an equivalent rewrite:
prefix = c if a < b < c else '1' + b
suffix = '00' if a < c else '000'
print(prefix + suffix)
a < c will evaluate to either True or False. So you are effectively getting either print('000'[True:]) or print('000'[False:]).
When you have the [] after a string, those will perform a slice on the string. You'd see this in actually practice as something like the following (here's a link for more info:
'abcde'[2] # returns 'c', which, starting from zero, is 2nd item
'abcde'[0] # returns 'a', which is the zero-th item
'abcde'[1:3] # returns 'bc', starting from the 1st item and going to the 3rd, not inclusive of the third
It looks like if you use booleans in such a slice, the False acts as a 0 and True acts as a 1
"abcde"[True] == "abcde"[1] # evaluates to true, these are the same
'abcde'[True] # evaluates to 'b'
"abcde"[False] == "abcde"[2] # evaluates to `False`
"abcde"[True] == "abcde"[2] # evaluates to `False`
"abcde"[False] == "abcde"[0] # evaluates to True, because False is effectively 0 here
So, having a [True:] or [False:] in that string-slice is the same as having [1:] or '[0:]`, which is saying to "give all characters starting with the second (for True/1:) or first (for False/0:) in this string".
The string '000' has length 3. The boolean a < c has value False or True. The operation s[i] for a string s and integer i refers to the i'th element of s, and the operation s[i:] takes the substring of s from index i through the end of s. A boolean value will be converted to an integer in Python as follows: False becomes 0 and True becomes 1.
So, '000'[a < c:] is the same as '000'[0:] if a < c is False, and this is the same as '000'. If a < c is True, then '000'[a < c:] is the same as '000'[1:] which is '00'.

Infinite while loop implementing Remove function using Open Addressing on Hash Table - Python

I am trying to implement the remove function with linear probing on a hash table.
I am having a problem with that, when I try to call the function remove it enter an infinite loop.
How can I fix that problem??
I have written the function taking the pseudocode:
remove(t, k):
z = 0
repeat
x = hash_function (k, z)
if hash_table[x] == k:
hash_table[x] = deleted return x
z = z+1
until hash_table[x] == NIL or z == m
return NIL
This is the code into a class:
class lin_prob:
def __init__(self, table_size):
self.hash_table = [None]*table_size
self.n_prime = 109169
self.a = random.randint(2, self.prime-1)
self.b = random.randint(2, self.prime-1)
self.n_keys = 0
def remove(self, k):
z = 0
while self.hash_table[x] != None or z < len (hash_table):
x = self.hash_function(k)
if self.hash_table[x] == k:
self.hash_table[x] = "deleted"
return x
else :
z = z+1
return "NIL"
def hash_function(self, k):
return ((self.a*k + self.b) % self.prime_number) % len(self.hash_table)
def len(self):
return self.n_keys
If you want to negate the until condition to create a while condition, you must change the or to an and:
while self.hash_table[x] != None and z < len (hash_table):
...
Also, you code cannot work because x is unknown in your remove method.
The problem arises with the condition you put into the while loop
while self.hash_table[x] != None and z < len (hash_table):
because the negation of equal == is not equal !=. I believe it will fix your problem.

function return none instead of return number

I create a function that compare with x and y variable. Inside the function has a lots of nested elif to compare the x and y then return integer. The problem is right now, when it runs at the certain elif statement, it didn't execute the statement although the statement is correct.
def convertTo(self, x, y):
if( x == 0 & y == 0):
return 0
if( x == 0 & y == 1):
return 1
if( x == 0 & y == 2):
return 2
if( x == 0 & y == 3):
return 3
if( x == 1 & y == 0):
return 4 # Didn't return this line even though x = 1 and y = 0
else
return None
def main():
self.convertTo(0,0)
self.convertTo(0,1)
self.convertTo(0,2)
self.convertTo(0,3)
self.convertTo(1,0) # return None? Why?
You're performing a chained equality comparison which is not doing what you think it does. The bitwise & is performed first as it has a higher priority than ==.
Replace:
x == 1 & y == 0
# 1 == 1 & 0 == 0
# 1 == 0 == 0 False!
With:
x == 1 and y == 0
See: Operator precedence
In Python, "&" and "and" do two different things. "and" is what you should be using, "&" is a binary operator.
if
a = 0011 1100
and
b = 0000 1101
then
a&b = 0000 1100
See http://www.tutorialspoint.com/python/python_basic_operators.htm
You should use and instead of &, as & is a bitwise and.
Chaining multiple conditions in Python is generally done with an if-elif-else statement like below:
if a and b:
# a and b both was true
elif a and not b:
# a was true, but b wasn't
else:
# none of the conditions matched
In your code, if it wasn't for the return statement in each if, and the fact that you are checking the same two variables, it would be possible for two if statements to evaluate to true.
if a:
# this will run if a was true
if b:
# regardless of a this will run if b was true
else:
# regardless of a this will only run if b was false
Also, take a look at this: https://docs.python.org/3/tutorial/controlflow.html

How to use a variable to find another variable in a list - python

(Python 3.x)
z=[]
x=0
while 1==1:
x=x+1
y=1
z.append(x)
while y==1:
a = 0
b = 0
if z(a)==x:
print(x)
y = 2
elif x%z(a)!= 0:
a = a+1
elif b == 2:
y = 2
else:
b = b+1
So, I made a code to find all the prime numbers until python crashes. However, it relies on z(a) for it to work. The idea is that as "a" changes, it moves on in the list.
"z(a)" is where the error lies, so does anyone know a way to fix this?
z is a list. You can approach values inside it by indexes using the z[a] operator (and not z(a) which assumes a function call with a as parameter).
I've taken the liberty of using boolean variables, += operators and unpacking values:
z = []
x = 0
while True:
x += 1
y = True
z.append(x)
while y:
a, b = 0, 0
if z[a] == x:
print(x)
y = False
elif x % z[a]: a += 1
elif b == 2: y = False
else: b += 1
I believe that's what you want to achieve (infinitely incrementing prime generator):
def prime (n): return not any([n % i == 0 for i in range(2, n)])
x = 0
while True:
if prime(x): print(x)
x += 1

If..else works as OR, does it true?

Does if..elif statements works exactly as one OR statement?
For example does below if..elif:
if X == "a":
Y = 1
elif Z == "b" and V = "c":
Y = 1
Works as
if X == "a" or (Z == "b" and V == "c"):
Y = 1
Yes, in your specific case the functionality is the same. You would use an if/elif statement if you were going to do two different things based upon the condition which executes to True. For example,
if a == 'a':
y = 1
elif z == 'b' and v == 'c':
y = 2
However, since in both cases you are doing Y = 1, then
if x == 'a' or (z == 'b' and v == 'c'):
y = 1
works just fine and even is more appropriate.
The effect in your code is the same. But it doesn't work the same way. The or statement looks at the two statements and will return True if at least one of the statements are true. On the other hand, the if...elif statement would just go into one of the conditions. Either only into the if statement, or only into the elif statement.

Categories

Resources