1. def match_patterns(patterns, data):
  2. """
  3. Matches patterns of arrays within data, optimizing for memory.
  4. Args:
  5. patterns: A list of array patterns to search for. Each pattern
  6. is a tuple of (start_index, end_index) representing a range
  7. of indices within the data array.
  8. data: The data array to search within.
  9. Returns:
  10. A dictionary where keys are the pattern indices and values are the
  11. corresponding pattern ranges within the data. Returns an empty
  12. dictionary if no patterns are found.
  13. """
  14. results = {}
  15. data_len = len(data)
  16. for i, pattern in enumerate(patterns):
  17. start, end = pattern
  18. if 0 <= start <= end < data_len:
  19. results[i] = (start, end)
  20. return results

Add your comment