# HG changeset patch # User Paul Boddie # Date 1481403771 -3600 # Node ID 01ac965a92a9c153dad924f30cf4e99161b7f43d # Parent d75ff26f7c0aecf62eb0931261fde547cc105729 Implemented the all and any functions, adding some tests. diff -r d75ff26f7c0a -r 01ac965a92a9 lib/__builtins__/iterable.py --- a/lib/__builtins__/iterable.py Sat Dec 10 21:13:54 2016 +0100 +++ b/lib/__builtins__/iterable.py Sat Dec 10 22:02:51 2016 +0100 @@ -19,8 +19,25 @@ this program. If not, see . """ -def all(iterable): pass -def any(iterable): pass +def all(iterable): + + "Return whether all of the elements provided by 'iterable' are true." + + for i in iterable: + if not i: + return False + + return True + +def any(iterable): + + "Return whether any of the elements provided by 'iterable' are true." + + for i in iterable: + if i: + return True + + return False def enumerate(iterable, start=0): diff -r d75ff26f7c0a -r 01ac965a92a9 tests/logical.py --- a/tests/logical.py Sat Dec 10 21:13:54 2016 +0100 +++ b/tests/logical.py Sat Dec 10 22:02:51 2016 +0100 @@ -13,18 +13,36 @@ def j(a, b, c): return f(a, b, c) and g(a, b, c) or c -print f(0, 0, 0) # 0 -print f(1, 0, 1) # 0 -print f(1, 1, 1) # 1 -print g(0, 0, 0) # 0 -print g(1, 0, 0) # 1 -print g(0, 0, 1) # 1 -print h(0, 0, 0) # 0 -print h(0, 0, 1) # 1 -print h(1, 0, 0) # 0 -print i(0, 0, 0) # 0 -print i(0, 0, 1) # 0 -print i(1, 0, 0) # 1 -print j(0, 0, 0) # 0 -print j(0, 0, 1) # 1 -print j(1, 0, 0) # 0 +print f(0, 0, 0) # 0 +print f(1, 0, 1) # 0 +print f(1, 1, 1) # 1 + +print g(0, 0, 0) # 0 +print g(1, 0, 0) # 1 +print g(0, 0, 1) # 1 + +print h(0, 0, 0) # 0 +print h(0, 0, 1) # 1 +print h(1, 0, 0) # 0 + +print i(0, 0, 0) # 0 +print i(0, 0, 1) # 0 +print i(1, 0, 0) # 1 + +print j(0, 0, 0) # 0 +print j(0, 0, 1) # 1 +print j(1, 0, 0) # 0 + +# Test any and all functions. + +l = [0, 0, 1, 0, 0] +print any(l) # True +print all(l) # False + +l = [1, 1, "one", 1] +print any(l) # True +print all(l) # True + +l = [1, 1, "one", ""] +print any(l) # True +print all(l) # False