简体   繁体   中英

How can we find the permutations of a n-digit number?

I wrote the permutations for the 4-digit number using a list and loops in python.

a =list(input("enter a four digit number:"))
n=[]
for i in range (0,4):
    for j in range (0,4):
        if(j!=i):
            for k in range(0,4):
                if(k!=i and k!=j):
                    for w in range (0,4):
                        if(w!=i and w!=j and w!=k):
                            n.append(a[i]+""+a[j]+""+a[k]+""+a[w])


print(n)

If the input is 1234, the output will be the permutations of all 1234 ie, 24 permutations. Can someone help me with the permutations of the n-digit number? I would prefer to see a pythonic solution.

Permutate [1..N]

import itertools
N = 4 # pick a number to permutate [1..N]
print(list(itertools.permutations(range(1, N + 1))))

Now if you want to permutate an arbitrary list:

import itertools
sample = [1,5,6,2,1]
print(list(itertools.permutations(sample)))

For the conditions inside if loop the condition recursion is used, There should be dynamic number of for loops based on the length of the digits so loop recursion method is used. The digits are concatenated in the l variable When there are repeated digits in number set method can make them as distinct

#n digit number as input converted into list
f=list(input("enter any number:"))
#dynamic array for dynamic for loop inside recursion 
a=[0 for k in range(len(f))]

c=[]#list which is to be used for append for digits
ans=[]# result in which the 
# recursion for if loop inside for loop
#1st argument is fixed inside the loop
#2nd argument will be decreasing
def conditn(k,m):
    if(m==0):
        return 1
    if(m==1):
        if(a[k]!=a[0]):
            return 1
    if(a[k]!=a[m-1] and conditn(k,m-1)):
        return 1
#recursion for for loop
#1st argument y is the length of the number
#2nd argument is for initialization for the varible to be used in for loop
#3rd argument is passing the list c
def loop(y, n,c):


    if  n<y-1:
        #recursion until just befor the last for loop
        for a[n] in range(y):
            if(conditn(n,n)):

                loop(y, n + 1,c)


    else:
    # last for loop
        if(n==y-1):
            for a[n] in range(y):
            #last recursion of condition
                if(conditn(n,n)):
                #concatinating the individual number
                    concat=""
                    for i in range(y):
                        concat+=f[a[i]]+""
                    c.append(concat)

#returning the list of result for n digit number
    return c 
#printing the list of numbers after method call which has recursion within
#set is used used to convert any of the iterable to the
#distinct element and sorted sequence of iterable elements, 
for j in (set(loop(len(f),0,c))):
    print(j)
    def swapper(s , i,j) :
    lst = list(s)
    
    return "".join(lst)

def solve(string , idx, res) :
    if idx == len(string) :
        res.append(string)
        return 
    
    for i in range(idx, len(string )) :
        lst=list(string)
        lst[i],lst[idx]=lst[idx],lst[i]
        string = "".join(lst)
        solve(string,idx+1,res)
        lst=list(string)
        lst[i],lst[idx]=lst[idx],lst[i]
        string = "".join(lst)

                         
n = 3 
k = 3
lst = []
res = []
for i  in range(1,n+1) :
    lst.append(str(i))
string  = "".join(lst)
solve(string, 0 , res)
res.sort()

n is the number of digits you may want in your answer res will store all the permutations of the number This is a simple resursion solution: )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM