Exemplo n.º 1
0
def is_sorted(iterable, key=lambda x:x, reverse=False, expected_len=None):
    ' checks if an iterable is sorted (consumes the iterable) '
    if reverse:
        gen = (key(a) >= key(b) for a,b in pairwise(iterable))
    else:
        gen = (key(a) <= key(b) for a,b in pairwise(iterable))

    # pairwise results in one fewer value
    if expected_len is not None:
        expected_len -= 1

    return counted_all(gen, expected_len=expected_len)
Exemplo n.º 2
0
 def test_pairwise(self):
     l = xrange(3)
     self.assertEqual([(0,1), (1,2)], [x for x in pairwise(l)])