Skip to content

Instantly share code, notes, and snippets.

@dlbas
Created June 4, 2019 07:40
Show Gist options
  • Select an option

  • Save dlbas/5f1235f58c6a53d073881d0b6c08d369 to your computer and use it in GitHub Desktop.

Select an option

Save dlbas/5f1235f58c6a53d073881d0b6c08d369 to your computer and use it in GitHub Desktop.
sections_set = [
[[-0.35, 1], [1, 2], [3, 5], [4, 6], [7, 8]],
[[-4, 1], [3, 7], [8, 4]]
]
for sections in sections_set:
cur_final = 0
sections.sort(key=lambda x: x[0])
if len(sections) < 1:
break
final_sections = [sections[0]]
if len(sections) < 2:
break
for points in sections[1:]:
# идем по всем отрезкам, если новый отрезок выступает за предыдущий
# то сдвигаем конец текущего на новую точку, если новый отрезок вообще
# за пределами - добавляем старый в массив, текущим считаем новый
# может лежать внутри отрезка
# может выходить правой точкой за отрезок
# может выходить левой точкой за отрезок -- IMPOSSIBLE
# может лежать вне отрезка
cur_start, cur_end = final_sections[cur_final]
first, second = points
if first > second:
first, second = second, first
if first > cur_end:
final_sections.append([first, second])
cur_final += 1
continue
if first <= cur_end < second:
final_sections[cur_final][1] = second
print(sum(b - a for a, b in final_sections))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment