I tried to implement permutation in Python

Overview

In Python, I actually implemented permutations () in ʻitertools`. I haven't implemented anything as sophisticated as the original, but it spits out all the basic uncovered permutation combinations.

The author is a kindergarten graduate, so please forgive me for any mistakes in the article.

theory

permutatuin (2).png

Implementation

Since code readability is prioritized, there are some redundant parts.

def permutations(source):
    length = len(source)
    
    if length == 1:
        return [source]
    
    result = []
    
    for x in range(length):
        nxarray = source[x]
        nxlist = source[0:x] + source[x+1:length]

        for y in permutations(nxlist):
            nyarray = [nxarray] + y
            result.append(nyarray)
            
    return result

numbers = [1, 2, 3]
print(permutations(numbers)})
#result(6)
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

source = [1,2,3,4]

# for x in range(source)
#The nxlist passed as an argument to the call to the recurrence function is proportional to the increase in x.
[2,3,4]
[3,4]
[4]
#It looks like

It is actually processed as follows

 Called with: [1, 2, 3]
   -> nxlist: [] + [2, 3] = [2, 3]
 Called with: [2, 3]
   -> nxlist: [] + [3] = [3]
 Called with: [3]
   ->      y: [3]
   -> stored: [2, 3]
   -> nxlist: [2] + [] = [2]
 Called with: [2]
   ->      y: [2]
   -> stored: [3, 2]
      -> Result: [[2, 3], [3, 2]]
   ->      y: [2, 3]
   -> stored: [1, 2, 3]
   ->      y: [3, 2]
   -> stored: [1, 3, 2]
   -> nxlist: [1] + [3] = [1, 3]
 Called with: [1, 3]
   -> nxlist: [] + [3] = [3]
 Called with: [3]
   ->      y: [3]
   -> stored: [1, 3]
   -> nxlist: [1] + [] = [1]
 Called with: [1]
   ->      y: [1]
   -> stored: [3, 1]
      -> Result: [[1, 3], [3, 1]]
   ->      y: [1, 3]
   -> stored: [2, 1, 3]
   ->      y: [3, 1]
   -> stored: [2, 3, 1]
   -> nxlist: [1, 2] + [] = [1, 2]
 Called with: [1, 2]
   -> nxlist: [] + [2] = [2]
 Called with: [2]
   ->      y: [2]
   -> stored: [1, 2]
   -> nxlist: [1] + [] = [1]
 Called with: [1]
   ->      y: [1]
   -> stored: [2, 1]
      -> Result: [[1, 2], [2, 1]]
   ->      y: [1, 2]
   -> stored: [3, 1, 2]
   ->      y: [2, 1]
   -> stored: [3, 2, 1]
      -> Result: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

Final Result: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

Conclusion

It is better to use ʻitertools`.

Recommended Posts

I tried to implement permutation in Python
I tried to implement PLSA in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
I tried to implement a one-dimensional cellular automaton in Python
I tried to implement the mail sending function in Python
I tried to implement blackjack of card game in Python
I tried to implement PCANet
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to implement StarGAN (1)
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to implement a card game of playing cards in Python
I want to easily implement a timeout in python
I tried to implement Minesweeper on terminal with python
I tried to implement an artificial perceptron with python
I tried to summarize how to use pandas in python
I tried to implement Deep VQE
I tried to touch Python (installation)
I tried to implement adversarial validation
I tried to implement hierarchical clustering
I tried to implement Realness GAN
I tried Line notification in Python
I tried to implement merge sort in Python with as few lines as possible
I tried to implement what seems to be a Windows snipping tool in Python
I tried to create API list.csv in Python from swagger.yaml
I tried "How to get a method decorated in Python"
I tried to make a stopwatch using tkinter in python
I tried to summarize Python exception handling
Python3 standard input I tried to summarize
I tried using Bayesian Optimization in Python
I wanted to solve ABC159 in Python
I tried to implement CVAE with PyTorch
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
[Python] I tried to implement stable sorting, so make a note
Implement Enigma in python
I tried Python> autopep8
Implement recommendations in Python
Implement XENO in python
I tried to debug.
I tried to paste
Implement sum in Python
I tried Python> decorator
Implement Traceroute in Python 3
I want to do Dunnett's test in Python
Try to implement Oni Maitsuji Miserable in python
How to implement Discord Slash Command in Python
I was able to recurse in Python: lambda
I want to create a window in Python
I tried playing a typing game in Python
How to implement shared memory in Python (mmap.mmap)
I tried to integrate with Keras in TFv1.1
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I wrote "Introduction to Effect Verification" in Python