[英]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.我使用 python 中的列表和循环编写了 4 位数字的排列。
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.如果输入是 1234,则 output 将是所有 1234 的排列,即 24 个排列。 Can someone help me with the permutations of the n-digit number?有人可以帮我解决n位数的排列吗? I would prefer to see a pythonic solution.我希望看到一个 pythonic 解决方案。
Permutate [1..N]置换 [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.对于 if 循环内的条件,使用条件递归,根据数字的长度应该有动态的 for 循环数,因此使用循环递归方法。 The digits are concatenated in the l variable When there are repeated digits in number set method can make them as distinct数字在 l 变量中连接当数字集方法中有重复数字时可以使它们不同
#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: ) n 是您在答案中可能需要的位数 res 将存储数字的所有排列 这是一个简单的 resursion 解决方案:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.