0

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 i.e., 24 permutations. Can someone help me with the permutations of the n-digit number? I would prefer to see a pythonic solution.

  • Does this answer your question? [Python get all permutations of numbers](https://stackoverflow.com/questions/2052951/python-get-all-permutations-of-numbers) – Joe Apr 11 '20 at 15:15

3 Answers3

1

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)))
Pani
  • 1,317
  • 1
  • 14
  • 20
0

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)
jatinkumar
  • 113
  • 7
  • your code is nice in terms of python I would also prefer for any problem, python gives a robust and less number of lines – jatinkumar Apr 11 '20 at 14:48
0
    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 : )

Auxer
  • 15
  • 3
  • You do not use your `k` variable or the `swapper` function, and do not explain your logic and algorithm, as a result your answer is currently unclear. You can find information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – AlexK Jun 23 '22 at 22:39