Given a list of
lts2 = [
[10, 0, 0, 'H', 9, 11],[8, 2, 2, 'B', 7, 5],[1, 6, 6, 'A', 2, 4],[11, 6, 6, 'F', 10, 5],
[13, 8, 8, 'G', 14, 16],[14, 8, 8, 'U', 13, 15],[15, 8, 8, 'J', 14, 16],[3, 10, 10, 'P', 2, 4],
[4, 10, 10, 'C', 3, 1],[7, 10, 10, 'T', 6, 8],[5, 12, 12, 'I', 12, 8],[12, 12, 12, 'W', 11, 3],
[2, 16, 16, 'D', 1, 9],[6, 18, 18, 'V', 5, 7],[16, 18, 18, 'Q', 15, 13],[9, 24, 24, 'E', 10, 12]
]
I need to sort so that the lists in the neighborhood do not have the same values i.e. s[i][4:]
should have no common values with s[i+1][:1]
and s[i-1][:1]
.
But every fourth value should be compared not with the fifth, but with the first.
The comparison should go like this 15->14->13->12->15
Next 11->10->9->8->11
. The output is a list of lists.
The code below is what I was able to do. But this is sorting in order.
a = 1
b = 0
while(a <= len(lst2)):
if(len(lst2[len(lst2)-a-1-b][4:]) == len(set(lst2[len(lst2)-a-1-b][4:])-set(lst2[len(lst2)-a][:1]))):
if(b > 0):
lst2[len(lst2)-a-1-b],lst2[len(lst2)-a-1] = lst2[len(lst2)-a-1],lst2[len(lst2)-a-1-b]
a = a + 1
b = 0
else:
b = b + 1
Given a list of
lts2 = [
[10, 0, 0, 'H', 9, 11],[8, 2, 2, 'B', 7, 5],[1, 6, 6, 'A', 2, 4],[11, 6, 6, 'F', 10, 5],
[13, 8, 8, 'G', 14, 16],[14, 8, 8, 'U', 13, 15],[15, 8, 8, 'J', 14, 16],[3, 10, 10, 'P', 2, 4],
[4, 10, 10, 'C', 3, 1],[7, 10, 10, 'T', 6, 8],[5, 12, 12, 'I', 12, 8],[12, 12, 12, 'W', 11, 3],
[2, 16, 16, 'D', 1, 9],[6, 18, 18, 'V', 5, 7],[16, 18, 18, 'Q', 15, 13],[9, 24, 24, 'E', 10, 12]
]
I need to sort so that the lists in the neighborhood do not have the same values i.e. s[i][4:]
should have no common values with s[i+1][:1]
and s[i-1][:1]
.
But every fourth value should be compared not with the fifth, but with the first.
The comparison should go like this 15->14->13->12->15
Next 11->10->9->8->11
. The output is a list of lists.
The code below is what I was able to do. But this is sorting in order.
a = 1
b = 0
while(a <= len(lst2)):
if(len(lst2[len(lst2)-a-1-b][4:]) == len(set(lst2[len(lst2)-a-1-b][4:])-set(lst2[len(lst2)-a][:1]))):
if(b > 0):
lst2[len(lst2)-a-1-b],lst2[len(lst2)-a-1] = lst2[len(lst2)-a-1],lst2[len(lst2)-a-1-b]
a = a + 1
b = 0
else:
b = b + 1
Share
Improve this question
edited Mar 23 at 13:30
Ch3steR
20.7k4 gold badges31 silver badges65 bronze badges
asked Mar 23 at 13:06
Alex SamAlex Sam
1
2
- You mention "sorting" as a goal, but I don't think that this is helpful. It is indeed a task that involves ordering of a sequence, but it is not a classical sorting task, so existing algorithms for sorting won't help you. I think, that instead you could look at scheduling algorithms. Scheduling is a more general task than sorting and you will find lots of tutorials on this topic. – Ulrich Eckhardt Commented Mar 23 at 15:18
- You could make your issue clearer by showing what the desired output is for the given input – Adon Bilivit Commented Mar 23 at 17:22
1 Answer
Reset to default 0I think the current approach returns a list with many repetitions. Although there might be useful to apply a different approach, where we ensure neighboring lists do not have common values in the specified positions and handle the special comparison rule for every fourth value:
def sort_list(lst):
def has_common_values(lst1, lst2):
return bool(set(lst1[4:]) & set(lst2[:1]))
sorted_lst = [lst[0]]
remaining_lst = lst[1:]
while remaining_lst:
last_element = sorted_lst[-1]
found = False
for i, elem in enumerate(remaining_lst):
if not has_common_values(last_element, elem):
if len(sorted_lst) > 1:
if has_common_values(sorted_lst[-2], elem):
continue
if len(sorted_lst) % 4 == 0:
if has_common_values(sorted_lst[-4], elem):
continue
sorted_lst.append(elem)
remaining_lst.pop(i)
found = True
break
if not found:
sorted_lst.append(remaining_lst.pop(0))
return sorted_lst