Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
This is the algorithm I'd use. It doesn't check for contradictions as I haven't found an optimal way of doing so. 1. Let n be the number of tasks. Create a list from 1 to n. 2. Iterate over each pair of instructions (e.g., (6, 2), (2, 5), (5, 4)) and do the following: 2.1. Let i,j be the indeces of each element of the pair. For example: List: [1, 2, 3, 4, 5, 6, 7] Pair: (6, 2) Indeces: (5, 1) # 0 based 2.2. If i<j skip to the next pair. Otherwise, place the first element at index j (i.e., before the second element) and continue with the next pair. For example: Old list: [1, 2, 3, 4, 5, 6, 7] New list: [1, 6, 2, 3, 4, 5, 7] 3. Print the list.
25th Aug 2019, 2:24 AM
Diego
Diego - avatar