def test_get_min_index(self): self.assertEquals( get_min_index([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, 5, 9]), 0) self.assertEquals( get_min_index([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [9, 5, 1]), 2) self.assertEquals( get_min_index([[1, 2, 3], [4, 5, 6], [7, 8, 0]], [9, 5, 1]), 1) self.assertEquals( get_min_index([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [3, 5, 9]), 1) self.assertEquals( get_min_index([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [3, 6, 9]), None)
def shortest_term_span(positions): """ Given a list of positions in a corpus, returns the shortest span of words that contain all query terms. """ # Initialize our list of lists where each list corresponds # to the locations within the document for a term indices = [0] * len(positions) min_window = window = get_window(positions, indices) # Iteratively moving the minimum index forward finds us our # minimum span while True: min_index = get_min_index(positions, window) if min_index is None: return sorted(min_window) indices[min_index] += 1 window = get_window(positions, indices) if list_range(min_window) > list_range(window): min_window = window if list_range(min_window) == len(positions): return sorted(min_window)