Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Permutations: producing cycle notation

$
0
0

Please consider the following problem: I have a certain permutation sigma:

sigma = [4,1,6,2,3,5]

the desired result is to produce the cycle notation which follows:

my_cycles_perm = [[4,2,1],[6,5,3]]

My attempt goes as per the code below, however it seems I only get as far as the first cycle but can't seem to reengage into the second one:

The idea behind the my_row_cycle function is to take a certain permutation sigma, set up a sort of circuit breaker called marker(crcuit closed when marker == 0), and iterate over the permutation until I complete a cycle, once the cycle is complete I store it into a list.

I then verify if there are still other cycles to extract from the permutation by iterating over sigma again until I find a number in sigma that isn't in the cycles extracted prior. If such a number is found I restart the process. If not I trip my circuit breaker, marker == 1 to end the entire process and output my cycle notation of the sigma permutation.

but this still seems to be utopia for me. :)

def my_row_cycle(sigma):    aux_sigma = list(sigma)    init_ref = aux_sigma.index(aux_sigma[0]) +1       #First antecedent of aux_sigma    init_image = aux_sigma[init_ref-1]                #Image of the antecedent    jumper_image = init_image    row_cycle = []    row_cycle.append(init_image)    my_cycles_perm = []    marker = 0    while marker == 0:                                 #Circuit breaker        while jumper_image != init_ref:                #establishes if cycle complete            for x in aux_sigma:                     #iterates sigma while cycle incomplete                jumper_ref = aux_sigma.index(x) + 1                if jumper_ref == jumper_image:         #Condition to append to cycle                    row_cycle.append(x)                    jumper_image = x                   #Changing the while loop condition        my_cycles_perm.append(row_cycle)        for a in aux_sigma:            for a in my_cycles_perm:                cycle = a                for a in cycle:                  #looking for match in aux_sigma and cycle                    if a not in cycle:                        row_cycle = []                        init_image = a                        init_ref = aux_sigma.index(init_image) + 1                        marker = 0                        break                    else:                        marker = 1return init_ref, init_image, jumper_image, jumper_ref, row_cycle, marker, my_cycles_perm

after evaluation:

(1, 4, 1, 6, [4, 2, 1], 1, [[4, 2, 1]])

I can't seem to understand why my marker trips to the value "1" and yet my cycle notation is incomplete. I thank you in advance if you have any suggestions and or corrections.


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>