daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. input:
  2. [(4,8),(6,10),(11,12),(15,20),(20,25)]
  3. output:
  4. [(4, 12), (15, 25)]
  5. input:
  6. ([(4,8),(6,10),(11,12),(15,20)])
  7. output:
  8. [(4, 12), (15, 20)]
  9.      
  10. import numpy as np
  11.  
  12. def simplify_intervals(intervals):
  13.     # Make intervals into an array
  14.     intervals = np.asarray(intervals)
  15.     # Make array for zero to the greatest interval end
  16.     r = np.arange(intervals[:, 1].max() + 1)[:, np.newaxis]
  17.     # Check what elements of the array are within each interval
  18.     m = (r >= intervals[:, 0]) & (r <= intervals[:, 1])
  19.     # Collapse belonging test for each value
  20.     ind = m.any(1).astype(np.int8)
  21.     # Find where the belonging test changes
  22.     d = np.diff(np.concatenate([[0], ind, [0]]))
  23.     # Find interval bounds
  24.     start = np.where(d > 0)[0]
  25.     end = np.where(d < 0)[0] - 1
  26.     # Make final intervals array
  27.     return np.stack([start, end], axis=1)
  28.  
  29. print(simplify_intervals([(4, 8), (6, 10), (11, 12), (15, 20), (20, 25)]))
  30. # [[ 4 12]
  31. #  [15 25]]
  32. print(simplify_intervals(([(4,8),(6,10),(11,12),(15,20)])))
  33. # [[ 4 12]
  34. #  [15 20]]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top