Skip to content

Instantly share code, notes, and snippets.

@gj84
Created February 28, 2014 04:27
Show Gist options
  • Save gj84/9265254 to your computer and use it in GitHub Desktop.
Save gj84/9265254 to your computer and use it in GitHub Desktop.
def nessitas_one(iterable): #fixed
"""Return the object in the given iterable that evaluates to True.
If the given iterable has more than one object that evaluates to True,
or if there is no object that fulfills such condition, return None.
>>> nessitas_one((True, False, False))
True
>>> nessitas_one((True, False, True))
False
>>> nessitas_one((0, 0, 'a'))
'a'
>>> nessitas_one((0, False, None))
False
>>> bool(nessitas_one((True, True)))
False
>>> bool(nessitas_one((False, True)))
True
>>> nessitas_one(())
False
"""
the_one = False
for i in iterable:
if i:
if the_one:
return False
the_one = i
return the_one
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment