Advertisement
pegorino

lines

Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. A, B = 15, 165
  2. N_1 = 37, 68
  3. N_2 = 52, 74
  4. N_3 = 118, 146
  5. N_4 = 35, 44
  6. N_5 = 37, 65
  7. N_6 = 46, 74
  8.  
  9. data = [N_1, N_2, N_3, N_4, N_5, N_6]
  10.  
  11.  
  12. def get_free_length(a, b, data=None):
  13.     # Для ручного воода
  14.     if not data:
  15.         data = []
  16.         n = int(input('number of lines: '))
  17.         for _ in range(n):
  18.             data.append(tuple(map(int, input('line ("," - splitter): ').split(','))))
  19.  
  20.     # "разворачиваем" основной отрезок вправо
  21.     if a > b:
  22.         a, b = b, a
  23.  
  24.     # "разворачиваем" все дополнительные отрезки вправо
  25.     data = list(map(lambda x: (x[0], x[1]) if x[0] < x[1] else (x[1], x[0]), data))
  26.  
  27.     sorted_data = sorted(data)
  28.  
  29.     free_length = 0
  30.  
  31.     # самое маленькое начало из вспомогательных отрезков
  32.     min_start = sorted_data[0][0]
  33.     # самое большое окончание из вспомогательных отрезков
  34.     max_end = max([i[1] for i in sorted_data])
  35.  
  36.     # проверяем, что есть наложения вспомогательных отрезков на основной
  37.     if b <= min_start or a >= max_end:
  38.         free_length = b - a
  39.     else:
  40.         # проверяем наложение в начале и в конце отрезка
  41.         if min_start > a:
  42.             free_length += min_start - a
  43.         if max_end < b:
  44.             free_length += b - max_end
  45.  
  46.         # значение текущего максимального конца отрезка
  47.         current_max_end = sorted_data[0][1]
  48.  
  49.         # сравниваем значение текущего максимального конца отрезка со следующим отрезком
  50.         for i in range(len(sorted_data) - 1):
  51.             if current_max_end < sorted_data[i + 1][0]:
  52.                 free_length += sorted_data[i + 1][0] - current_max_end
  53.             if current_max_end < sorted_data[i + 1][1]:
  54.                 current_max_end = sorted_data[i + 1][1]
  55.  
  56.     return free_length
  57.  
  58.  
  59. print(get_free_length(a=A, b=B, data=data))
  60. print(get_free_length(a=B, b=A, data=data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement