@@ -0,0 +1,2582 @@
+from operator import add, eq%0Aimport unittest%0A%0Adef squash(predicate, list_):%0A    %22%22%22Squash multiple consecutive list elements into one%0A%0A    For each sequence of elements, optionally interspersed with whitespace or%0A    empty strings, for which the predicate returns True, take the first element%0A    in the sequence and drop the rest.%0A%0A    Parameters:%0A        predicate -- function of one argument returning True for squashable%0A                     elements%0A%0A        list_     -- list of elements to modify%0A%0A    Return value:%0A        A new list. The input list remains intact.%0A%0A    %22%22%22%0A%0A    def find(index, list_):%0A        %22%22%22Find the first element which satisfies the predicate%0A%0A        Return the index of the first element in list_%5Bindex:%5D for which%0A        predicate(elem) is True%0A%0A        %22%22%22%0A        for i in range(index, len(list_)):%0A            if predicate(list_%5Bi%5D):%0A                return i%0A        return -1%0A%0A    result = list_%5B:%5D%0A    base = 0%0A    while True:%0A        base = find(base, result)%0A        if base %3C 0:%0A            break%0A%0A        n = find(base+1, result)%0A        if n %3C 0:%0A            break%0A%0A        if not reduce(add, result%5Bbase+1:n%5D, '').strip():%0A            # There are either no elements or only whitespace and empty string%0A            # elements between base and n.%0A            result%5Bbase+1:n+1%5D = %5B%5D%0A        else:%0A            base = n%0A%0A    return result%0A%0A%0A### TESTS ###%0A%0Adef isint(x):%0A    return type(x) is int%0A%0A%0Aclass SquashTest(unittest.TestCase):%0A    def test_empty_list(self):%0A        list_ = %5B%5D%0A        self.assertEqual(list_, squash(isint, list_))%0A%0A    def test_one_element(self):%0A        for list_ in %5B%5B1%5D, %5B'a'%5D, %5BNone%5D, '', ' '%5D:%0A            self.assertEqual(list_, squash(isint, list_))%0A%0A    def test_no_dupes(self):%0A        list_ = %5B1, '2', 3, '4', ' ', 5, '6', ' ', 7, '8', '6', 9%5D%0A        self.assertEqual(list_, squash(isint, list_))%0A%0A    def test_all_dupes(self):%0A        for list_ in %5B%5B1%5D * 10, %5B1, '', ' ', ' ', 2, '', 3%5D%5D:%0A            self.assertEqual(%5B1%5D, squash(isint, list_))%0A%0A    def test_range(self):%0A        list_ = %5B' ', '', 1, 2, 3, '', 4, ' '%5D%0A        self.assertEqual(%5B' ', '', 1, ' '%5D, squash(isint, list_))%0A%0A    def test_known_inputs(self):%0A        known = %5B%0A            (%5B2, 1, 0, 'a'%5D, %5B2, 'a'%5D),%0A            (%5B'a', 3, 2, 'b'%5D, %5B'a', 3, 'b'%5D),%0A            (%5B2, 1%5D, %5B2%5D),%0A            (%5B'a', 'b'%5D, %5B'a', 'b'%5D),%0A            (%5B1, 'a', 2, 'b', 3, 4%5D, %5B1, 'a', 2, 'b', 3%5D),%0A        %5D%0A        for given, answer in known:%0A            self.assertEqual(answer, squash(isint, given))%0A%0Aif __name__ == '__main__':%0A    unittest.main()%0A

Add 'squash' helper module