Monday, 11 July 2016

Enumerating Gene Orders

In this problem we are given the task to find all the possible permutations of a list from 1 to n, where n ≤ 7. We are given n and are supposed to return the total number of permutations and a list of all the different permutations. To clarify, if we are told that n = 3, then the total number of permutations is 3! which is equal to 6 and we should print the following:

6      
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

It was fairly easy to make this program since Python3 has a built in library for factorials and another built in library for making permutations. The following is the final code:

import math                                         
import itertools                                    
n = 3                                               
print(math.factorial(n))                            
perm = itertools.permutations(list(range(1, n + 1)))
for i, j in enumerate(list(perm)):                  
    permutation = ''                                
    for item in j:                                  
        permutation += str(item) + ' '              
    print(permutation)                              

No comments:

Post a Comment