Cara menggunakan permutation string python

Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.

Table of Contents

  • Permutation 
  • Combination 
  • Python - 76 karakter
  • J, 11 karakter
  • Python - 55 karakter
  • Haskell, 44 43
  • dalam Q (48)
  • Ruby - 23 karakter
  • Ruby - 59 karakter
  • C, 270 243 239 karakter
  • JS - 154 146 karakter
  • Scala 195, quick'n'dirty, tanpa permutasi dari perpustakaan:
  • Scala 293, dewasa, jenis iterator aman:
  • Python - 50 karakter
  • Pyth, 4 byte
  • JavaScript 143 136 134 123
  • Python, 53 byte
  • K (oK) , 3 byte
  • Aksioma, 160 byte
  • Japt , 1 byte
  • APL (NARS), 39 karakter, 78 byte
  • 05AB1E - 2 1 byte s

Permutation 

First import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form. 
 

Python3

from itertools import permutations 

perm = permutations([1, 2, 3]) 

for i in list(perm): 

    print (i) 

Output: 

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

It generates n! permutations if the length of the input sequence is n. 
If want  to get permutations of length L then implement it in this way. 
 

Python3

from itertools import permutations 

perm = permutations([1, 2, 3], 2

for i in list(perm): 

    print (i) 

Output: 

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)

It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.

Combination 

This method takes a list and an input r as an input and return an object list of tuples which contain all possible combination of length r in a list form. 
 

Python3

from itertools import combinations

comb = combinations([1, 2, 3], 2)

for i in list(comb):

    print (i)

Output: 

(1, 2)
(1, 3)
(2, 3)

1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order. 
 

Python3

from itertools import combinations 

comb = combinations([1, 2, 3], 2

for i in list(comb): 

    print (i)

Output: 

(1, 2)
(1, 3)
(2, 3)

2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination. 
 

Python3

from itertools import combinations 

comb = combinations([2, 1, 3], 2

for i in list(comb): 

    print (i)

Output: 

(2, 1)
(2, 3)
(1, 3)

3. If we want to make a combination of the same element to the same element then we use combinations_with_replacement. 
 

Python3

from itertools import combinations_with_replacement 

comb = combinations_with_replacement([1, 2, 3], 2

for i in list(comb): 

    print (i) 

Output:

(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 

Cara menggunakan permutation string python


Tulis fungsi yang mengambil sebagai input satu set bilangan bulat (bisa berupa daftar, larik atau wadah lain dengan angka yang berbeda), dan menampilkan daftar semua permutasi.

Python (95 karakter) :

p=lambda s:s and sum(map(lambda e:map(lambda p:[e]+p,p(filter(lambda x:x!=e,s))),s),[]) or [[]]

Akan menyenangkan bisa dikalahkan dalam bahasa yang sama, tetapi implementasi dalam bahasa lain lebih dari diterima!

Jawaban:


Python - 76 karakter

Lebih panjang dari gnibbler, tetapi mengimplementasikan sesuatu dari awal.

p=lambda x:x and[[a]+b for a in x for b in p([c for c in x if c!=a])]or[[]]



J, 11 karakter

([email protected][email protected]#A.[)

Pemakaian:

   ([email protected][email protected]#A.[) 1 3 5
1 3 5
1 5 3
3 1 5
3 5 1
5 1 3
5 3 1

Penjelasan:

[email protected][email protected]# menggunakan tiga kata kerja untuk mengembalikan daftar dari 0 ke (! n) -1 di mana n adalah jumlah item dalam daftar yang diberikan.

[mengembalikan daftar itu sendiri. Dalam contoh yang ditunjukkan itu memberi 0 1 2 3 4 5 A. 1 3 5.

A.mengembalikan satu kemungkinan permutasi dari daftar kedua untuk setiap item dalam daftar pertama (jenis - penjelasan yang tepat diberikan di sini ).




Python - 55 karakter

from itertools import*
p=lambda x:list(permutations(x))



Haskell, 44 43

p[]=[[]]
p l=[e:r|e<-l,r<-p$filter(/=e)l]

Pada dasarnya sama dengan solusi ugoren, tetapi Haskell lebih baik dalam hal pemahaman daftar!


Tentu saja bisa juga

30

import Data.List
p=permutations

Pendekatan yang lebih efisien, yang tidak memerlukan perbandingan kesetaraan:

92

import Data.List
p[]=[[]]
p l=(\(l,(e:r))->map(e:)$p(l++r))=<<(init$zip(inits l)(tails l))

Sebagai akibatnya, yang ini juga berfungsi ketika ada elemen duplikat dalam daftar.






dalam Q (48)

g:{$[x=1;y;raze .z.s[x-1;y]{x,/:y except x}\:y]}

Penggunaan sampel:

q)g[3;1 2 3]
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Ruby - 23 karakter

f=->x{p *x.permutation}

misalnya f[[1,2,3]] keluaran ini .

tetapi menggunakan [].permutation rasanya seperti curang, jadi:

Ruby - 59 karakter

f=->a{a.size<2?[a]:a.flat_map{|x|f[(a-x=[x])].map{|y|x+y}}}

diuji dengan

100.times.all?{arr=(1..99).to_a.sample(rand(5)); arr.permutation.to_a==f[arr]}
=> true




Python - 58 karakter

Sedikit lebih pendek dari ugoren, dengan mengambil set sebagai input:

p=lambda x:x and[[y]+l for y in x for l in p(x-{y})]or[[]]

C, 270 243 239 karakter

#define S t=*a;*a=a[i];a[i]=t;
#define R o=p(n,r-1,a+1,o,r-2,0)
int*p(n,r,a,o,i,t)int*a,*o;{if(!r)for(;n;--n)*o++=*--a;else{R;for(;i;--i){S R;S}}return o;}
P(n,a)int*a;{int N=1,i=n;for(;i;N*=i--);return p(n,n,a,malloc(N*n*8),n-1,0)-N*n;}

Fungsi P (n, a) mengembalikan pointer ke n! permutasi dari, dikemas satu demi satu dalam satu susunan raksasa.




K, 30 byte

{[email protected]@&((#x;1)~^=:)'v:!(#x)##x}

Tidak ada builtin!


JS - 154 146 karakter

function f(x){var a=[],m;(m=x.length)>1?f(x.slice(1)).map(function(y){for(l=m;l--;a.push(y.slice(0,l).concat(x[0],y.slice(l))));}):a=[x];return a}

Tes: f([1,2,3,4,5]).map(function(a){return a.join('')}).join('\n')mengembalikan ini .


R

Karena kita berbicara tentang permutasi, izinkan saya menunjukkan setidaknya satu solusi di R:

library(gtools);v=c(3,4,5);permutations(length(v),length(v),v)

Perl 188

Tidak ada rutinitas perpustakaan, tidak ada rekursi

sub p{$l=(@_=sort split'',shift)-1;while([email protected]_){$k=$j=$l;--$k while($_[$k-1]cmp$_[$k])>=0;$k||last;--$j while($_[$k-1]cmp$_[$j])>=0;@_[$j,$k-1][email protected]_[$k-1,$j];@_[$k..$l][email protected]_[$k..$l]}}

Scala 30:

def p(s:Seq[_])=s.permutations 

Scala 195, quick'n'dirty, tanpa permutasi dari perpustakaan:

def c(x:Int,t:List[_]):List[_]={val l=t.size
val o=x%l
if(l>1){val r=c(x/l,t.tail)
r.take(o):::(t.head::r.drop(o))}else
t}
def p(y:List[_])=(0 to(1 to y.size).product).foreach(z=>println(c(z,y)))

val y=List(0,1,2,3)
p(y)

Scala 293, dewasa, jenis iterator aman:

class P[A](val l:Seq[A])extends Iterator[Seq[A]]{
var c=0
val s=(1 to l.size).product
def g(c:Int,t:List[A]):List[A]={
val n=t.size
val o=c%n
if(n>1){val r=g(c/n,t.tail)
r.take(o):::(t.head::r.drop(o))
}else
t}
def hasNext=c!=s
def next={c+=1
g(c-1,l.toList)}
}
for(e<-new P("golf"))println(e)

Python - 50 karakter

import itertools
list(itertools.permutations("123"))


Pyth, 4 byte

L.pb

Ya, Pyth dibuat setelah tantangan ini diposting dan semuanya. Ini masih sangat keren. : D

Demo langsung.

Membaca dari stdin lebih pendek satu byte:

.pQ

JavaScript 143 136 134 123

function p(s,a="",c="",i,z=[]){a+=c,i=s.length
!i?z.push(a):0
for(;i--;s.splice(i,0,c))p(s,a,c=s.splice(i,1),0,z);return z}

var perms = p([1,2,3]);

document.getElementById('output').innerHTML = perms.join("\n");
 id="output">




Python, 53 byte

from itertools import*;lambda x:list(permutations(x))



K (oK) , 3 byte

Larutan

prm

Cobalah online!

Penjelasan:

Ini adalah 3 byte built-in pintas ke berikut built-in 47 fungsi byte:

{[x]{[x]$[x;,/x ,''o'x ^/:x;,x]}@$[-8>@x;!x;x]}

... yang dapat disingkat menjadi 23 byte jika kita tahu kita mendapatkan daftar int sebagai input:

{$[x;,/x,''o'x^/:x;,x]} / golfed built in
{                     } / lambda function with implicit input x
 $[ ;             ;  ]  / if[condition;true;false]
   x                    / if x is not null...
             x^/:x      / x except (^) each-right (/:) x; create length-x combinations
           o'           / call self (o) with each of these
       x,''             / x concatenated with each-each of these results (this is kinda magic to me)
     ,/                 / flatten list
                    ,x  / otherwise enlist x (enlisted empty list)

Aksioma, 160 byte

p(a)==(#a=0=>[[]];r:=[[a.1]];r:=delete(r,1);n:=#a;m:=factorial n;m>1.E7=>r;b:=permutations n;for j in 1..m repeat(x:=b.j;r:=concat([a.(x.i)for i in 1..n],r));r)

ungolfed

--Permutation of a
pmt(a)==
     #a=0=>[[]]
     r:=[[a.1]]; r:=delete(r,1) -- r has the type List List typeof(a)
     n:=#a
     m:=factorial n
     m>1.E7=>r
     b:=permutations(n)         --one built in for permutation indices 
     for j in 1..m repeat
        x:=b.j
        r:=concat([a.(x.i) for i in 1..n],r)
     r

Semua ini memanggil satu fungsi perpustakaan yang memberikan permutasi pada indeks (hanya integer sebagai permutasi sebagai permutasi pada [1], permutasi pada [1,2], permutasi pada [1,2,3] dll). Jadi cukup dapatkan set ini indeks dan membangun daftar; Kita harus mencatat bahwa ini tampaknya dikompilasi dengan baik untuk setiap Daftar tipe X

(4) -> p([1,2,3])
   Compiling function p with type List PositiveInteger -> List List
      PositiveInteger
   (4)  [[1,2,3],[1,3,2],[3,1,2],[2,1,3],[2,3,1],[3,2,1]]
                                          Type: List List PositiveInteger
(5) -> p([x^2,y*x,y^2])
   Compiling function p with type List Polynomial Integer -> List List
      Polynomial Integer
   (5)
      2      2    2  2        2  2            2  2        2  2    2      2
   [[x ,x y,y ],[x ,y ,x y],[y ,x ,x y],[x y,x ,y ],[x y,y ,x ],[y ,x y,x ]]
                                       Type: List List Polynomial Integer
(6) -> p([sin(x),log(y)])
   Compiling function p with type List Expression Integer -> List List
      Expression Integer
   (6)  [[sin(x),log(y)],[log(y),sin(x)]]
                                       Type: List List Expression Integer
(7) -> m:=p("abc")::List List Character
   Compiling function p with type String -> Any
   (7)  [[a,b,c],[a,c,b],[c,a,b],[b,a,c],[b,c,a],[c,b,a]]
                                                Type: List List Character
(8) -> [concat(map(x+->x::String, m.j))  for j in 1..#m]
   (8)  ["abc","acb","cab","bac","bca","cba"]
                                                        Type: List String


Japt , 1 byte

á

Japt penerjemah

Ini terbentur dan tidak memiliki jawaban Japt, jadi saya pikir saya akan melanjutkan dan menambahkan satu. áketika diterapkan ke array dan tanpa argumen apa pun adalah builtin untuk "dapatkan semua permutasi". The -Rbendera yang digunakan di link interpreter hanya memodifikasi bagaimana hasilnya dicetak.


APL (NARS), 39 karakter, 78 byte

{1≥k←≢w←,⍵:⊂w⋄↑,/{w[⍵],¨q w[a∼⍵]}¨a←⍳k}

uji:

  q←{1≥k←≢w←,⍵:⊂w⋄↑,/{w[⍵],¨q w[a∼⍵]}¨a←⍳k}
  q 1 2 3
1 2 3  1 3 2  2 1 3  2 3 1  3 1 2  3 2 1 
  q 'abcd'
abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 

05AB1E - 2 1 byte s

œ

Input harus berupa array / daftar.

Penjelasan:

œ //Takes all the permutations of the elements in the top of the stack (the input is a list, so it would work)

Menyimpan satu byte berkat Erik the Outgolfer