# HG changeset patch # User Paul Boddie # Date 1481393106 -3600 # Node ID 2ab8235da0608202ee225b24dad1d23d30850fb8 # Parent 2eb43643f5eddd59b24753eba21ac5cc8cc7fce6 Implemented enumerate, filter, map, reversed and sum. Fixed max and min. Extended the tests to cover these functions. diff -r 2eb43643f5ed -r 2ab8235da060 lib/__builtins__/iterable.py --- a/lib/__builtins__/iterable.py Sat Dec 10 18:54:07 2016 +0100 +++ b/lib/__builtins__/iterable.py Sat Dec 10 19:05:06 2016 +0100 @@ -21,8 +21,39 @@ def all(iterable): pass def any(iterable): pass -def enumerate(iterable): pass -def filter(function, sequence): pass + +def enumerate(iterable, start=0): + + """ + Iterate over 'iterable', obtaining items and combining them with position + information, producing a sequence containing tuples of the form + (position, item). The first position is indicated by 'start' (which is zero + by default) and each subsequent positions is incremented from the one + preceding it. + """ + + l = [] + pos = start + + for i in iterable: + l.append((pos, i)) + pos += 1 + + return l + +def filter(function, sequence): + + """ + Apply 'function' to each element in 'sequence', returning a sequence of all + elements for which the result of the function evaluated to a true value. + """ + + l = [] + for i in sequence: + if function(i): + l.append(i) + return l + def iter(collection): "Implementation of iter without callable plus sentinel support." @@ -35,32 +66,56 @@ return obj.__len__() -def map(function, *args): pass +def map(function, sequence): + + """ + Apply 'function' to each element of 'sequence' in turn, appending the result + to a new sequence containing all results. + """ -def max(*args): + l = [] + for i in sequence: + l.append(function(i)) + return l + +def max(args): "Implementation of max." - highest = args[0] - for arg in args[1:]: - if arg > highest: + highest = None + for arg in args: + if highest is None or arg > highest: highest = arg return highest -def min(*args): +def min(args): "Implementation of min." - lowest = args[0] - for arg in args[1:]: - if arg > lowest: + lowest = None + for arg in args: + if lowest is None or arg < lowest: lowest = arg return lowest def reduce(function, sequence, initial=None): pass -def reversed(sequence): pass + +def reversed(sequence): + + "Return a reversed version of the given 'sequence'." + + return sequence[::-1] + def sorted(iterable, cmp=None, key=None, reverse=False): pass -def sum(sequence, start=0): pass + +def sum(sequence, start=0): + + "Sum the elements in 'sequence', adding to any indicated 'start' value." + + total = start + for i in sequence: + total += i + return total def zip(args): diff -r 2eb43643f5ed -r 2ab8235da060 tests/range.py --- a/tests/range.py Sat Dec 10 18:54:07 2016 +0100 +++ b/tests/range.py Sat Dec 10 19:05:06 2016 +0100 @@ -1,18 +1,27 @@ l = range(0, 10) -print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -print len(l) # 10 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +print len(l) # 10 x = xrange(0, -10, -2) -print x # __builtins__.span.xrange(0, -10, -2) -print len(x) # 5 +print x # __builtins__.span.xrange(0, -10, -2) +print len(x) # 5 for i in x: - print i # 0 - # -2 - # -4 - # -6 - # -8 + print i # 0 + # -2 + # -4 + # -6 + # -8 x = xrange(0, -10, 2) -print x # __builtins__.span.xrange(0, 0, 2) -print len(x) # 0 +print x # __builtins__.span.xrange(0, 0, 2) +print len(x) # 0 + +y = xrange(4, 8) +print y # __builtins__.span.xrange(4, 8, 1) +print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)] +print sum(y) # 22 +print map(lambda x: x*2, y) # [8, 10, 12, 14] +print filter(lambda x: x%2, y) # [5, 7] +print max(y) # 7 +print min(y) # 4 diff -r 2eb43643f5ed -r 2ab8235da060 tests/slice.py --- a/tests/slice.py Sat Dec 10 18:54:07 2016 +0100 +++ b/tests/slice.py Sat Dec 10 19:05:06 2016 +0100 @@ -11,3 +11,5 @@ print l[1:9:2] # [1, 3, 5, 7] print l[9:1:-2] # [9, 7, 5, 3] print l[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] +print l[::-2] # [9, 7, 5, 3, 1] +print reversed(l) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]