1.1 --- a/lib/__builtins__/iterable.py Sat Dec 10 18:54:07 2016 +0100
1.2 +++ b/lib/__builtins__/iterable.py Sat Dec 10 19:05:06 2016 +0100
1.3 @@ -21,8 +21,39 @@
1.4
1.5 def all(iterable): pass
1.6 def any(iterable): pass
1.7 -def enumerate(iterable): pass
1.8 -def filter(function, sequence): pass
1.9 +
1.10 +def enumerate(iterable, start=0):
1.11 +
1.12 + """
1.13 + Iterate over 'iterable', obtaining items and combining them with position
1.14 + information, producing a sequence containing tuples of the form
1.15 + (position, item). The first position is indicated by 'start' (which is zero
1.16 + by default) and each subsequent positions is incremented from the one
1.17 + preceding it.
1.18 + """
1.19 +
1.20 + l = []
1.21 + pos = start
1.22 +
1.23 + for i in iterable:
1.24 + l.append((pos, i))
1.25 + pos += 1
1.26 +
1.27 + return l
1.28 +
1.29 +def filter(function, sequence):
1.30 +
1.31 + """
1.32 + Apply 'function' to each element in 'sequence', returning a sequence of all
1.33 + elements for which the result of the function evaluated to a true value.
1.34 + """
1.35 +
1.36 + l = []
1.37 + for i in sequence:
1.38 + if function(i):
1.39 + l.append(i)
1.40 + return l
1.41 +
1.42 def iter(collection):
1.43
1.44 "Implementation of iter without callable plus sentinel support."
1.45 @@ -35,32 +66,56 @@
1.46
1.47 return obj.__len__()
1.48
1.49 -def map(function, *args): pass
1.50 +def map(function, sequence):
1.51 +
1.52 + """
1.53 + Apply 'function' to each element of 'sequence' in turn, appending the result
1.54 + to a new sequence containing all results.
1.55 + """
1.56
1.57 -def max(*args):
1.58 + l = []
1.59 + for i in sequence:
1.60 + l.append(function(i))
1.61 + return l
1.62 +
1.63 +def max(args):
1.64
1.65 "Implementation of max."
1.66
1.67 - highest = args[0]
1.68 - for arg in args[1:]:
1.69 - if arg > highest:
1.70 + highest = None
1.71 + for arg in args:
1.72 + if highest is None or arg > highest:
1.73 highest = arg
1.74 return highest
1.75
1.76 -def min(*args):
1.77 +def min(args):
1.78
1.79 "Implementation of min."
1.80
1.81 - lowest = args[0]
1.82 - for arg in args[1:]:
1.83 - if arg > lowest:
1.84 + lowest = None
1.85 + for arg in args:
1.86 + if lowest is None or arg < lowest:
1.87 lowest = arg
1.88 return lowest
1.89
1.90 def reduce(function, sequence, initial=None): pass
1.91 -def reversed(sequence): pass
1.92 +
1.93 +def reversed(sequence):
1.94 +
1.95 + "Return a reversed version of the given 'sequence'."
1.96 +
1.97 + return sequence[::-1]
1.98 +
1.99 def sorted(iterable, cmp=None, key=None, reverse=False): pass
1.100 -def sum(sequence, start=0): pass
1.101 +
1.102 +def sum(sequence, start=0):
1.103 +
1.104 + "Sum the elements in 'sequence', adding to any indicated 'start' value."
1.105 +
1.106 + total = start
1.107 + for i in sequence:
1.108 + total += i
1.109 + return total
1.110
1.111 def zip(args):
1.112
2.1 --- a/tests/range.py Sat Dec 10 18:54:07 2016 +0100
2.2 +++ b/tests/range.py Sat Dec 10 19:05:06 2016 +0100
2.3 @@ -1,18 +1,27 @@
2.4 l = range(0, 10)
2.5 -print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.6 -print len(l) # 10
2.7 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.8 +print len(l) # 10
2.9
2.10 x = xrange(0, -10, -2)
2.11 -print x # __builtins__.span.xrange(0, -10, -2)
2.12 -print len(x) # 5
2.13 +print x # __builtins__.span.xrange(0, -10, -2)
2.14 +print len(x) # 5
2.15
2.16 for i in x:
2.17 - print i # 0
2.18 - # -2
2.19 - # -4
2.20 - # -6
2.21 - # -8
2.22 + print i # 0
2.23 + # -2
2.24 + # -4
2.25 + # -6
2.26 + # -8
2.27
2.28 x = xrange(0, -10, 2)
2.29 -print x # __builtins__.span.xrange(0, 0, 2)
2.30 -print len(x) # 0
2.31 +print x # __builtins__.span.xrange(0, 0, 2)
2.32 +print len(x) # 0
2.33 +
2.34 +y = xrange(4, 8)
2.35 +print y # __builtins__.span.xrange(4, 8, 1)
2.36 +print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)]
2.37 +print sum(y) # 22
2.38 +print map(lambda x: x*2, y) # [8, 10, 12, 14]
2.39 +print filter(lambda x: x%2, y) # [5, 7]
2.40 +print max(y) # 7
2.41 +print min(y) # 4
3.1 --- a/tests/slice.py Sat Dec 10 18:54:07 2016 +0100
3.2 +++ b/tests/slice.py Sat Dec 10 19:05:06 2016 +0100
3.3 @@ -11,3 +11,5 @@
3.4 print l[1:9:2] # [1, 3, 5, 7]
3.5 print l[9:1:-2] # [9, 7, 5, 3]
3.6 print l[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
3.7 +print l[::-2] # [9, 7, 5, 3, 1]
3.8 +print reversed(l) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]