title: Merging overlapping interval objects with dependencies
body: <p>i need to merge interval objects to get distinct ranges of intervals based on extra parameters. How is the best way to do that?</p>
  
  <p>It's about unambiguous statement whether in a given hour state is true. The returned list must have non-duplicated intervals.</p>
  
  <p>Interval object description:</p>
  
  <pre class="lang-py prettyprint-override"><code>{
      'startDate': datetime.datetime, # start of interval
      'endDate': datetime.datetime, # end of interval
      'prioritized': bool # if True - it's always important, override no-prioritized intervals
      'state': bool # result of interval
  }
  </code></pre>
  
  <p>In the examples below i changed startDate/endDate to strings to make them look better.</p>
  
  <p>Interval list look like:</p>
  
  <pre class="lang-py prettyprint-override"><code>interval_list = [
      {'startDate': '10:00:00', 'endDate': '12:00:00', 'prioritized': False, 'state': False},
      {'startDate': '11:00:00', 'endDate': '18:00:00', 'prioritized': True, 'state': True},
      {'startDate': '13:00:00', 'endDate': '17:00:00', 'prioritized': False, 'state': False},
      {'startDate': '17:00:00', 'endDate': '20:00:00', 'prioritized': False, 'state': True},
      {'startDate': '19:30:00', 'endDate': '19:45:00', 'prioritized': True, 'state': False}
  ]
  </code></pre>
  
  <p>I am trying to achieve the following:</p>
  
  <p><code>merge(interval_list)</code> should return:</p>
  
  <pre class="lang-py prettyprint-override"><code>[
      {'startDate': '10:00:00', 'endDate': '11:00:00', 'state': False},
      {'startDate': '11:00:00', 'endDate': '19:30:00', 'state': True},
      {'startDate': '19:30:00', 'endDate': '19:45:00', 'state': False},
      {'startDate': '19:45:00', 'endDate': '20:00:00', 'state': True},
  ]
  </code></pre>
  
  <p>I have following not completed code right now:</p>
  
  <pre class="lang-py prettyprint-override"><code>def merge_range(ranges: list):
      ranges = sorted(ranges, key=lambda x: x['startDate'])
      last_interval = dict(ranges[0])
  
      for current_interval in sorted(ranges, key=lambda x: x['startDate']):
          if current_interval['startDate'] &gt; last_interval['endDate']:
              yield dict(last_interval)
              last_interval['startDate'] = current_interval['startDate']
              last_interval['endDate'] = current_interval['endDate']
              last_interval['prioritized'] = current_interval['prioritized']
              last_interval['state'] = current_interval['state']
          else:
              if current_interval['state'] == last_interval['state']:
                  last_interval['endDate'] = max(last_interval['endDate'], current_interval['endDate'])
              else:
                  pass # i stopped here
  
      yield dict(last_interval)
  </code></pre>
  
  <p>And use it by <code>merged_interval_list = list(merge_range(interval_list))</code></p>
  
  <p>Is it a good way ? </p>