Faisal Afzal
Faisal Afzal

Reputation: 273

Evaluating the output of multiple functions which return True or False

So I came across this interesting problem.It basically is a lot of functions which return True or False within a function and I want that secondary function to return either True or False based on applying an AND or OR logic to all of the functions within it. I know this is a terrible way to explain so let's see some code which will hopefully explain it better

#this is the first function that return True or False
def f(x):
    if x == 1:
        return True
    elif x == 0:
        return False
#this is the function that takes the first one and I want it to return either True or False based on an AND logic
def g(f):

    f(1)
    f(0)
    f(0)
    f(1)
    f(1)
    f(0)
    f(1)

Now I know I can just write the second function with 'and' between all the f(x) functions that I call but that seems very ugly and so I want something that will just evaluate all of these and return me a value. I don't enough experience with writing methods which take in multiple inputs and also multiple inputs that vary so I would appreciate any help on this.

Upvotes: 1

Views: 814

Answers (3)

cglacet
cglacet

Reputation: 10942

You can use the existing all function that is equivalent to a logical AND:

def f(x):
  return x < 5

all((f(1), f(2), f(3), f(4)))

Now concerning function g you can do this (for example):

def g(f, inputs):
  for i in inputs:
    yield f(i)

all(g(f, range(5)))

Here you can replace range(5) with any of [0, 1, 2, 3, 4], (0, 1, 2, 3, 4), {0, 1, 2, 3, 4}, and many more (ie. any iterable).

Note that a function similar to g also exists in python, it's called map, you could use it this way:

all(map(f, range(5))) # More or less equivalent to all(g(f, range(5))) 

You could also directly make use a generator expression (an alternative to the yield generator form):

all(f(i) for i in range(5))

Which one of this solution is the best really depend on the use case and on your personal preferences (even if the last one is probably the one you will most commonly see).

Upvotes: 1

Netwave
Netwave

Reputation: 42716

You can use all and a comprehension over the variable arguments (*args) of the funtion:

>>> def f(x):
...     if x == 1:
...         return True
...     elif x == 0:
...         return False
... 
>>> def g(f, *args):
...     return all(f(x) for x in args)
... 
>>> g(f, 1, 0, 0, 1)
False
>>> g(f, 1, 1, 1)
True

Upvotes: 2

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

For AND function, you can use python's all, and for OR function, you can use python's any

>>> all([True, False])
False
>>> all([True, True])
True
>>> any([True, False])
True
>>> any([True, True])
True
>>> any([False, False])
False

Just append all your outputs in a list, and evaluate all or any, so considering function f you defined

print(all([f(1),f(1)]))
print(all([f(0),f(0)]))
print(any([f(1), f(0)]))
print(any([f(0), f(0)]))
#True
#False
#True
#False

Upvotes: 0

Related Questions