Given a list of input (let’s say they are just integers), and a list of functions (and these functions takes an integer, and returns either True or False).
I have to take this list of input, and see if any function in the list would return True for any value in the list.
Is there any way to do this faster than O(n^2)
Right now what I have is
for v in values:
for f in functions:
if f(v):
# do something to v
break
Any faster methods?
Without any further information on the functions, the results of the
len(functions) * len(values)possible function calls must be considered independent from each other, so there is no faster way than checking them all.You can write this a little more concisely, though:
The builtin function
any()also short-circuits, just like your original code.Edit: It turns out that the desired equivalent would have been
See the comments for a discussion.