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]
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@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 -R
bendera 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