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)
def test_list_range(self): self.assertEquals(list_range([5, 6, 10]), 5) self.assertEquals(list_range([1, 7, 9]), 8)
def test_list_range(self): self.assertEquals( list_range([5, 6, 10]), 5) self.assertEquals( list_range([1, 7, 9]), 8)