I start with a list full of False
elements.
Then those elements are switched to True
independently over the course of iterations.
I need to know when the list is fully True.
Let's say I have 3 elements, they start as
[False, False, False]
then I check them over the iterations like:
elements == [True, True, True]
The list of elements is fixed and should not grow (or shrink). You can think of these elements as switches, the input determines how many there are and they start all switched off. The only thing that can happen over time is that individual switches are turned on (True) by events happening in the iteration.
How does python do the checking and what is the cost?
What is the best way in terms of cost to check that?
Is there a way with bit operations or anything that checks all the elements at once?
Python “is” operator can be used to efficiently check for the equality of two string objects. The is operator returns True if the two variables point to the same data object, else, it returns False.
Technique 2: Python ‘!=’ operator for String comparison. Python ‘!=’ operator can also be used to perform a string equals check in python. The '!=' operator compares two strings and returns True if the strings are unequal, otherwise, it returns False.
In order to have a caseless string comparison, i.e. in a case-insensitive manner, then we can use Python string.casefold () function to serve the purpose. The string.casefold () method converts the string to lowercase instantly. In the scenario of string comparison, we can pass both of the input strings to the casefold () function.
Equality is basically just asking if the contents of the two object are the same and in the case of lists, it needs to be in the same order as well. Identity in Python refers to the object you are referring to. In Python, the identity of an object is a unique, constant integer (or long integer) that exists for the length of the object's life.
Use all
,
>>> l = [True, True, True]
>>> all(l)
True
Notice that if the iterable is empty, it will return True
as well.
>>> all([])
True
You could create your own flag class, one which implements the idea of @StefanPochmann and keeps track of how many flags have been set.
Proof of concept:
class flags:
def __init__(self,n):
self.__flags = [False]*n
self.__ntrue = 0
def flags(self):
return self.__flags[:] #so read only
def __len__(self):
return len(self.__flags)
def check(self,i):
return self.__flags[i]
def on(self,i):
if not self.check(i):
self.__ntrue +=1
self.__flags[i] = True
def off(self,i):
if self.check(i):
self.__ntrue -=1
self.__flags[i] = False
def toggle(self,i):
if self.check(i):
self.off(i)
else:
self.on(i)
def ntrue(self):
return self.__ntrue
Tested like:
import random
i = 0
f = flags(5)
while f.ntrue() != len(f):
i +=1
f.toggle(random.randint(0,4))
print(i,f.flags())
Typical output:
19 [True, True, True, True, True]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With