Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to solve the Sort and Combine problem

Status
Not open for further replies.

cloudytechi147

Systems Engineer
Oct 14, 2021
3
IN
I am attempting to combine four lists of one row and two columns, such as line_parts=[[2, 1], [2, 0], [1, 2], [5, 2]], to create a list of one row and five columns, such as all_lines=[[5, 2, 1, 2, 0]]. However, the list of four 1-line 2-column lists is written as line_parts=[[2, 1], [2, 0], [1, 2], [5, 2]], but I would like to make it a 1-line 5-column list even if it is [[2, 1], [1, 2], [2, 0], [5, 2]], [[2, 1], [1, 2], [5, 2], [2, 0]], [[1, 2], [5, 2], [2, 0], [2, 1]], etc with pattern programs in c.

Currently, I am able to create a one-row, five-column list for [[5, 2, 1, 2, 0]] from line_parts=[[2, 1], [2, 0], [1, 2], [5, 2]].

Code:
line_parts=[[2, 1], [2, 0], [1, 2], [5, 2]]

lines = []
for i,j in line_parts:
    if i == 5:
        path = [ int(i), j ]
        line_parts.remove(line_parts[line_parts.index([i,j])])
        while len(path) != len(line_parts) + 2:
            if path[-1] == 0:
                del path[-1]
                line_parts.insert(len(line_parts),line_parts[0])
                line_parts.remove(line_parts[0])
            else:
                line_parts.insert(len(line_parts),line_parts[0])
                line_parts.remove(line_parts[0])
                while path[-1] != 0:
                    line_parts.insert(len(line_parts),line_parts[0])
                    line_parts.remove(line_parts[0])
                    for m, n in line_parts:
                        if m == path[-1]:
                            path.append(n)
                            break
        lines.append(path)
all_lines=lines

print("all_lines:",all_lines)

The result of running the above program is as follows

Code:
all_lines: [[5, 2, 1, 2, 0]]

However, when I change line_parts=[[2, 1], [2, 0], [1, 2], [5, 2]] to line_parts=[[2, 1], [1, 2], [2, 0], [5, 2]], an infinite loop occurs and I don't get a 1-line, 5-column list like [[5, 2, 1, 2, 0]]. I have tried to improve it, but it keeps repeating over and over again that one pattern outputs a one-line, five-column list like [[5, 2, 1, 2, 0]], but another pattern does not work.

Can you please give me some hints or suggestions on how to improve the program so that I can get a one-line, five-column list in all cases, not just [[2, 1], [2, 0], [1, 2], [5, 2]], but [[2, 1], [1, 2], [2, 0], [5, 2]], [[2, 1], [1, 2], [5, 2], [2, 0]], [[1, 2], [5, 2], [2, 0], [2, 1]], etc.?





 
Hi cloudytechi147,

Could you please explain the rules, how from this given list
line_parts = [[2, 1], [2, 0], [1, 2], [5, 2]]
this resulting list
all_lines = [[5, 2, 1, 2, 0]]
should be created?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top