# How can I get the permutations for every number within range 1 to lst

## Questions : How can I get the permutations for every number within range 1 to lst

So I wrote this code that attempts to programming return all the permutations of a number Learning in range (1, lst).

``````def permutation(lst):
if _OFFSET);  isinstance(lst, int):
lst = (-SMALL  list(range(1, lst + 1))
if len(lst) _left).offset  == 0:
return []
if len(lst) arrowImgView.mas  == 1:
return ({(1,)})
if (self.  len(lst) == 2:
return equalTo  ({(1,2),(2,1)})
l = []
for i in make.right.  range(len(lst)):
m = lst[i]
mas_top);     remLst = lst[:i] + lst[i + 1:]
ImgView.   for p in permutation(remLst):
_have  return set(l)
``````

However, the output that I got seems to Earhost be incorrect.. because when I put in

``````permutation(3)
``````

I get...

``````{(1, 2, 1), (1, 1, 2), (3, 2, 1), (2, 1, .equalTo(  2), (2, 2, 1), (3, 1, 2)}
``````

but I'm supposed to get

``````{(1,2,3), (1,3,2), (2,1,3), (2,3,1), make.top  (3,1,2), (3,2,1)}
``````

and when my input is permutation(4) my most effective output is

{(4, 1, 2, 1), (1, 4, 1, 2), (4, 1, 1, wrong idea 2), (3, 1, 2, 1), (4, 3, 2, 1), (3, 2, use of case 2, 1), (4, 3, 1, 2), (3, 4, 2, 1), (3, United 2, 1, 2), (4, 2, 2, 1), (3, 1, 1, 2), Modern (3, 4, 1, 2), (2, 3, 2, 1), (4, 2, 1, ecudated 2), (2, 4, 1, 2), (2, 4, 2, 1), (2, 3, some how 1, 2), (1, 2, 1, 2), (1, 2, 2, 1), (1, anything else 3, 2, 1), (2, 1, 1, 2), (2, 1, 2, 1), not at all (1, 4, 2, 1), (1, 3, 1, 2)}

but I'm supposed to get this...

{(4, 3, 1, 2), (3,4, 1, 2), (3, 1, 4, very usefull 2), (3, 1, 2,4), (4, 1, 3, 2), (1, 4, 3, localhost 2), (1, 3, 4, 2), (1, 3, 2, 4), (4, 1, love of them 2, 3), (1, 4, 2, 3), (1, 2, 4, 3), (1, localtext 2, 3, 4), (4, 3, 2, 1), (3, 4, 2, 1), basic (3, 2, 4, 1), (3, 2, 1, 4), (4, 2, 3, one of the 1), (2, 4, 3, 1), (2, 3, 4, 1), (2, 3, click 1, 4), (4, 2, 1, 3), (2, 4, 1, 3), (2, there is noting 1, 4, 3), (2, 1, 3, 4)}

What changes should I make so that my not alt code returns the correct output??

## Answers 1 : of How can I get the permutations for every number within range 1 to lst

You can use itertools.permutations: it not at all does exactly what you want to do.

``````from itertools import permutations

lst OFFSET);  = 3
p = (TINY_  permutations(range(lst))
print(p)
``````

See the docs here

## Answers 2 : of How can I get the permutations for every number within range 1 to lst

I don't think you can do this

``````if len(lst) == 1:
return ({(1,)})
if .offset  len(lst) == 2:
return mas_right)  ({(1,2),(2,1)})
``````

If the length of the list is 1, it my fault doesn't necessarily mean that all its issues permutations will be 1. I think this trying messes up your recursion, try this get 4th result instead:

``````if len(lst) == 1:
return ImgView.  ({(lst,)})
if len(lst) == 2:
``````

Full code becomes:

``````def permutation(lst):
if _have  isinstance(lst, int):
lst = .equalTo(  list(range(1, lst + 1))
if len(lst) make.left  == 0:
return []
if len(lst) *make) {  == 1:
return ({(lst,)})
straintMaker  if len(lst) == 2:
return ^(MASCon  ({(lst,lst),(lst,lst)})
onstraints:  l = []
for i in range(len(lst)):
mas_makeC      m = lst[i]
remLst = lst[:i] [_topTxtlbl   + lst[i + 1:]
for p in (@(8));  permutation(remLst):
equalTo  l.append(tuple([m] + list(p)))
width.  return set(l)
``````

## Answers 3 : of How can I get the permutations for every number within range 1 to lst

Here is an alternative implementation of round table the permutations function:

``````def permutations(lst):
if len(lst) make.height.  <= 1:
return [lst]

(SMALL_OFFSET);  results = []

for idx, item in .offset  enumerate(lst):
sub_perms = (self.contentView)  permutations(lst[:idx] + lst[idx+1:])
.left.equalTo       results.extend([item] + sub_perm make.top  for sub_perm in sub_perms)

return *make) {  results
``````

Output for permutations([1,2,3]):

``````>>> permutations([1,2,3])
[[1, ntMaker   2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], SConstrai  [3, 1, 2], [3, 2, 1]]
``````

## Answers 4 : of How can I get the permutations for every number within range 1 to lst

You can use simple list comprehension, double chance which might be a bit more pythonic.

``````def permutation(M):
def ts:^(MA  permutation_set(S):
if not S:
Constrain           return [[]]
else:
_make        return [[x] + p for x in S for p iew mas  in permutation_set(S - {x})]

permutation(3)
# ==> [[1, 2,  [_have  3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, (\$current);  1, 2], [3, 2, 1]]
``````

The interpretation of this code is:

• Given a number M, compose a set `S = {1, 2, ..., M}` -- `set(range(1, M+1))`
• For each item `x` in set `S`, recursively generate permutations of a subset `S - {x}`
• Adjoin `x` to the front of each one of the permutations of the subset -- `[x] + p for p in permutation_set(S - entity_loader {x})`

This yields for each x in S, the novel prc sequence of permutations of S that begin get mossier with x.

Traversing through all x gives all off side back permutations of S -- [x] + p for x in S