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.
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
fromitertools importpermutations
perm =permutations([1, 2, 3], 2)
fori inlist(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
fromitertools importcombinations
comb =combinations([1, 2, 3], 2)
fori inlist(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
fromitertools importcombinations
comb =combinations([1, 2, 3], 2)
fori inlist(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
fromitertools importcombinations
comb =combinations([2, 1, 3], 2)
fori inlist(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
fromitertools importcombinations_with_replacement
comb =combinations_with_replacement([1, 2, 3], 2)
fori inlist(comb):
print(i)
Output:
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
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[[]]
[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:
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)
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):0for(;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.