Thread: Joins transpositions into one single cycle!

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6

Joins transpositions into one single cycle!

Hey guys!

I was wondering, If I have a list of transpositions such this:

[(0, 11), (1, 5), (2, 18), (3, 21), (4, 24), (5, 7), (6, 0), (7, 12), (8, 25), (9, 4), (10, 14), (11, 9), (12, 3), (13, 2), (14, 27), (15, 6), (16, 10), (17, 16), (18, 1), (20, 8), (21, 23), (22, 15), (23, 19), (24, 26), (25, 22), (26, 13), (27, 20)]

what's the best way to join all of them into one cycle? - or ordered items - so it looks like this?

[17,16,10,14,27,20,8,25,22,15,6,0,11,9,4,24,26,13,2,18,1,5,7,12,3,21,23,19]
2. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1317
I can sort of guess at what you want to do, but you really need to explain your problem better.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
Originally Posted by Lux Perpetua
I can sort of guess at what you want to do, but you really need to explain your problem better.
ok basically I have a list of tuples, each tuple contains a pair of numbers which represent the sequential order of them in the bigger sequence, for example, (1,3),(3,2) : one comes before three and three comes before two so the final sequence will appear as 1,3,2.

the lower bound of the given sequence is 17 (there's no number that comes before 17) - no pair such as (x , 17) which means 17 comes first for sure in the sequence and so on following the same rule.

its not quite the most comfy problem to explain but i hope ive been clear enough )
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
114
Rep Power
7
Off the top of my head, here's a basic idea of how you could go at it:
Code:
```>>> d = dict([(0, 11), (1, 5), (2, 18), (3, 21), (4, 24), (5, 7)])
>>> d
{0: 11, 1: 5, 2: 18, 3: 21, 4: 24, 5: 7}
>>> d[0]
11```
And so forth. Make your list of tuples into a dictionary, then access it with your number as a key to get the number that comes next.

Comments on this post

• Lux Perpetua agrees : My solution would be similar.
5. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1317
Here's a little trick to figure out which element should come first:
Code:
```>>> input_list = [(0, 11), (1, 5), (2, 18), (3, 21), (4, 24), (5, 7),
...               (6, 0), (7, 12), (8, 25), (9, 4), (10, 14), (11, 9),
...               (12, 3), (13, 2), (14, 27), (15, 6), (16, 10),
...               (17, 16), (18, 1), (20, 8), (21, 23), (22, 15),
...               (23, 19), (24, 26), (25, 22), (26, 13), (27, 20)]
>>> next_map = dict(input_list)
>>> set(next_map.keys()) - set(next_map.values())
{17}
>>>```

Comments on this post

• Nightmareix35 agrees
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
Originally Posted by Nyktos
Off the top of my head, here's a basic idea of how you could go at it:
Code:
```>>> d = dict([(0, 11), (1, 5), (2, 18), (3, 21), (4, 24), (5, 7)])
>>> d
{0: 11, 1: 5, 2: 18, 3: 21, 4: 24, 5: 7}
>>> d[0]
11```
And so forth. Make your list of tuples into a dictionary, then access it with your number as a key to get the number that comes next.
Brilliant. Saved me from a 3 nested for loops. Exactly what I was looking for :]] thanks!