def ValidateBlocks(self, problems, trip_intervals_by_block_id):
    # Expects trip_intervals_by_block_id to be a dict with a key of block ids
    # and a value of lists of tuples
    # (trip, min_arrival_secs, max_departure_secs)

    # Cache potentially expensive ServicePeriod overlap checks
    service_period_overlap_cache = {}

    for (block_id,trip_intervals) in trip_intervals_by_block_id.items():

      # Sort trip intervals by min arrival time
      trip_intervals.sort(key=(lambda x: x[1]))

      for xi in range(len(trip_intervals)):
        trip_interval_a = trip_intervals[xi]
        trip_a = trip_interval_a[0]

        for xj in range(xi+1,len(trip_intervals)):
          trip_interval_b = trip_intervals[xj]
          trip_b = trip_interval_b[0]

          # If the last departure of trip interval A is less than or equal
          # to the first arrival of trip interval B, stop checking
          if trip_interval_a[2] <= trip_interval_b[1]:
            break

          # We have an overlap between the times in two trip intervals in
          # the same block.  Potentially a problem...

          # If they have the same service id, the trips run on the same
          # day, yet have overlapping stop times.  Definitely a problem.
          if trip_a.service_id == trip_b.service_id:
            problems.OverlappingTripsInSameBlock(trip_a.trip_id,
                                                 trip_b.trip_id, block_id)
          else:
            # Even if the the trips don't have the same service_id, their
            # service dates might still overlap.  Since the ServicePeriod
            # overlap check is potentially expensive, we cache the
            # computation

            service_id_pair_key = tuple(sorted([trip_a.service_id,
                                                trip_b.service_id]))

            # If the serivce_id_pair_key is not in the cache, we do the
            # full service period comparison
            if service_id_pair_key not in service_period_overlap_cache:
              
              # If the trip references an unknown service id, then we bail,
              # since we can't effectively determine block overlap and an
              # error will have already been registered for the missing
              # service id.
              if trip_a.service_id not in self.service_periods:
                return
              if trip_b.service_id not in self.service_periods:
                return

              service_period_a = self.GetServicePeriod(trip_a.service_id)
              service_period_b = self.GetServicePeriod(trip_b.service_id)

              dates_a = service_period_a.ActiveDates()
              dates_b = service_period_b.ActiveDates()

              overlap = False

              for date in dates_a:
                if date in dates_b:
                  overlap = True
                  break

              service_period_overlap_cache[service_id_pair_key] = overlap

            if service_period_overlap_cache[service_id_pair_key]:
              problems.OverlappingTripsInSameBlock(trip_a.trip_id,
                                                   trip_b.trip_id,
                                                   block_id)
dataset/ETHPy150Open google/transitfeed/transitfeed/schedule.py/Schedule.ValidateBlocks/original