I want the results of the function to be:
def AllTheSame(iterable):
return any(iterable) is all(iterable)
def unanimous(it):
it1, it2 = itertools.tee(it)
return all(it1) or not any(it2)
Piggybacking on Ignacio Vasquez-Abram's method, but will stop after first mismatch:
def unanimous(s):
it1, it2 = itertools.tee(iter(s))
it1.next()
return not any(bool(a)^bool(b) for a,b in itertools.izip(it1,it2))
While using not reduce(operators.xor, s)
would be simpler, it does no short-circuiting.
Not so brief, but shortcuts without messing around with 'tee' or anything like that.
def unanimous(s):
s = iter(s)
if s.next():
return all(s)
else:
return not any(s)
def all_equals(xs):
x0 = next(iter(xs), False)
return all(bool(x) == bool(x0) for x in xs)
def all_bools_equal(lst):
return all(lst) or not any(lst)
See: http://docs.python.org/library/functions.html#all
See: http://docs.python.org/library/functions.html#any