Demo entry 6768131

perm

   

Submitted by anonymous on Nov 01, 2018 at 14:07
Language: Python 3. Code size: 1.1 kB.

def permute(a,i):
    if i<len(a)-1:
        for j in range(i,len(a)):
            #Make a copy of the original list with two swapped elements.
            b=a.copy()
            b[i],b[j]=b[j],b[i]
            #Call function with new, swapped list and swap elements downstream of i (hence, i increased by 1)
            permute(b,i+1)
    else:
        string=''
        for k in range(len(a)):
            string=string+str(a[k])+' '
        out=open('C:/ROSALIND/20_Enumerating_gene_order/output.txt','a') #'a' for append, so the file is not overwritten
        out.write(string+'\n')
        out.close()

n=int(input('Enter number:')) #Get a number from the keyboard
nlist=list() #create list for numbers 1 through n
fact=1 #initiate a factorial for the number of combinations
for i in range(n): #build list and calculate number of combinations
    nlist.insert(i,i+1)
    fact=fact*(i+1)
out=open('C:/ROSALIND/20_Enumerating_gene_order/output.txt','w') #initiate an output file and write the number of combinations
out.write(str(fact)+'\n')
out.close()
permute(nlist,0) #start the permutation
print('Done!')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).