---
# System prepended metadata

title: Untitled

---


Given a set of time intervals, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. 


```
Input:

[[1, 5], [4, 7], [9, 10], [3, 4], [11, 14]]

Intermediate:


[[1, 5], [3, 4], [4, 7], [9, 10], [11, 14]]

Output:

[[1, 7], [9, 10], [11,14]]
```

For example, let the given set of intervals be {{1,3}, {5,7}, {2,4}, {6,8}}. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly, {5, 7} and {6, 8} should be merged and become {5, 8}


```python3
def summarise(input_ranges):
    # sort the input ranges by first member (start)
    # traverse sorted input and merge if needed
    # assert that it gives expected_output
    sorted_ranges = []
    sorted_ranges = sorted(sorted_ranges, key=lambda a: a[0])
    output_ranges = []
    for range in sorted_ranges:
        if len(output_ranges) == 0:
            output_ranges.append(range)
        else:
            last_range = output_ranges[-1]
            if range[0] <= last_range[1]:
                output_ranges[-1][1] = max(range[1], output_ranges[-1][1]) # (1,5), (3,4) -> (1,5)
            else:
                output_ranges.append(range)
    return output_ranges
    
    

input_ranges = [[1, 5], [4, 7], [9, 10], [3, 4], [11, 14]]
expected_output = [[1, 7], [9, 10], [11,14]]

assert summarise(input_ranges) == expected_output
```





You have N tasks to run in any order you want.
Each task takes one time step to run.
There must be a minimum of K (cooldown value) time steps between two runs of the same task.
Write a function that returns the sequence of tasks to run in a minimal amount of time steps.

```python3
1) tasks = ["A", "A", "B"], K = 3
["A", "B", "", "", "A"]
2) tasks = ["A","A","A","B","B","B"], K = 2
["A", "B", "", "A", "B", "", "A", "B"]
3) tasks = ["A", "B", "B", "C"], K = 4
["B", "A", "C", "", "", "B"]
```

```python3
K = 3
tasks = ["A", "A", "B"]

#tasks = ["A", "B", "C"]

N_diff = len(set(tasks))

from collections import Counter

task_count = Counter(tasks)
task_count.ordered_by_max()
max_task_number_per_group = task_count[0][1]

max_task_counter = 0

{"A": 2, "B" :2, "C" : 2}
K = 1

abcabca--

K = 4
["A", "A", "A", "A", "A", "B", "C", "D", "E", "F"]
[A, B, C, D, E, A, F---A, ---A, ----A]


K = 2
["A", "A", "A", "A", "A", "B", "C", "D", "E"]
[A, B, C,A,  D, E, A, -, -, A, -, -, A]

task_count = {"A": 5, "B": 1, ...}
sequence = []
K = 1
[A, ]
A -> 2
[A, B]
B -> 2
A -> 1
[A, B, C]
C -> 2
B -> 1
A -> 0
controler = [""] * K

["", "", ""]
["A", "", ""]
["B", "A", ""]
["C", "B", "A"]


cooldown = {"A": 0, "B": 0, "C": 0, "D": 0, "E": 0, }
while sum(task_count.values()) > 0:
    for task, value in cooldown.iteritems():
        if value == 0:
            current_task = task
        
    sequence.append(current_task)
    cooldown[current_task] = K + 1
    task_count[current_task] -= 1
    for task_name in set(tasks):
        cooldown[task_name] = max(0, cooldown[task_name] - 1)


for task_item in task_count.iteritems():
    if task_item[1] == max_task_number_per_group:
        max_task_counter += 1
    else:
        break
number_steps = max_task_counter * max(K + 1, N_diff) + max_task_counter

sequence = [0] * number_steps
index = 0
for task_item in task_count.iteritems():
    sequence[index:K+1:task_count[1]*(K+1)] = task_count[0]
    index += 1

1) tasks = ["A", "A", "B"], K = 3
["A", "B", "", "", "A"] 5
max_task_counter = 1
period = 4
4 + 1

1) tasks = ["A", "A", "B", "B"], K = 3
["A", "B", "", "", "A", "B"] 6
# B, A, _, _, _, A 6
max_task_counter = 2
period = 4
4 + 2

2) tasks = ["A","A","A","B","B","B"], K = 2
["A", "B", "", "A", "B", "", "A", "B"] 8
3) tasks = ["A", "B", "B", "C"], K = 4
["B", "A", "C", "", "", "B"] 6

```