On Fri, 29 Apr 2005 23:37:39 -0400, Anthony D'Agostino [EMAIL PROTECTED]
wrote:
I found my old bubble sort solution:
def esort(edges):
while 1:
swaps = 0
for j in range(len(edges)-2):
if edges[j][1] != edges[j+1][0]:
I need to sort this list:
[('A','Y'), ('J','A'), ('Y','J')] like this:
[('A','Y'), ('Y','J'), ('J','A')].
Note how the Ys and Js are together. All I need is for the second element of
one tuple to equal the first element of the next tuple. Another valid
solution is [('J','A'), ('A','Y'),
Sort demands a unique ordering, which isn't present in your case.
You're constructing an Eulerian path. See Fleury's algorithm:
http://en.wikipedia.org/wiki/Eulerian_path
--
http://mail.python.org/mailman/listinfo/python-list
In article [EMAIL PROTECTED],
Anthony D'Agostino [EMAIL PROTECTED] wrote:
I need to sort this list:
[('A','Y'), ('J','A'), ('Y','J')] like this:
[('A','Y'), ('Y','J'), ('J','A')].
Note how the Ys and Js are together. All I need is for the second element of
one tuple to equal the first
I found my old bubble sort solution:
def esort(edges):
while 1:
swaps = 0
for j in range(len(edges)-2):
if edges[j][1] != edges[j+1][0]:
edges[j+1],edges[j+2] = edges[j+2],edges[j+1] # swap