Trong ví dụ này, bạn sẽ học cách tính toán tất cả các hoán vị của chuỗi.
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Chuỗi Python
- Python cho vòng lặp
Hoán vị là phương pháp chọn các phần tử từ một tập hợp theo những cách khác nhau.
Ví dụ: số cách mà các ký tự từ yup
có thể được chọn là yup
, ypu
, ____10,
yup ypu uyp upy puy pyu None1,
yup ypu uyp upy puy pyu None2,
yup ypu uyp upy puy pyu None3 và không chọn bất kỳ.
Chúng tôi sẽ thực hiện tương tự trong các ví dụ sau.
Ví dụ 1: Sử dụng đệ quy
def get_permutation[string, i=0]:
if i == len[string]:
print["".join[string]]
for j in range[i, len[string]]:
words = [c for c in string]
# swap
words[i], words[j] = words[j], words[i]
get_permutation[words, i + 1]
print[get_permutation['yup']]
Đầu ra
yup ypu uyp upy puy pyu None
Trong ví dụ này, đệ quy được sử dụng để tìm các hoán vị của chuỗi yup
.
- IF điều kiện in
yup ypu uyp upy puy pyu None
5 được truyền dưới dạng đối số nếu nó bằng chiều dài củayup ypu uyp upy puy pyu None
6. - Trong mỗi lần lặp của vòng lặp, mỗi ký tự của
yup
được lưu trữ trongyup ypu uyp upy puy pyu None
8. - Các yếu tố của các từ được hoán đổi. Theo cách này, chúng tôi đạt được tất cả các kết hợp khác nhau của các nhân vật.
- Quá trình này tiếp tục cho đến khi đạt được độ dài tối đa.
Ví dụ 2: Sử dụng itertools
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
Đầu ra
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
Trong ví dụ này, đệ quy được sử dụng để tìm các hoán vị của chuỗi yup
.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Một hoán vị, còn được gọi là số sắp xếp của người Viking, hoặc đơn đặt hàng, là một sự sắp xếp lại các yếu tố của một danh sách được đặt hàng thành một thư từ một-một với chính S. Một chuỗi độ dài n có n! hoán vị. Ví dụ:
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA
Chúng tôi có giải pháp hiện tại cho vấn đề này, vui lòng giới thiệu các hoán vị của một chuỗi đã cho bằng liên kết STL. Chúng ta cũng có thể giải quyết vấn đề này trong Python bằng cách sử dụng các hoán vị chức năng sẵn có [có thể sử dụng được]. & NBSP;
Python3
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
0 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
1from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
2 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
4 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
5from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
7from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
8from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
9['pro', 'por', 'rpo', 'rop', 'opr', 'orp']0
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']1
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6['pro', 'por', 'rpo', 'rop', 'opr', 'orp']3
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
8['pro', 'por', 'rpo', 'rop', 'opr', 'orp']5
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']6
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']7
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']8
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']9
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA0
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA1
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA2
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA3
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA4
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']030
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA7
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6 ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']0
ABC ACB BAC BCA CAB CBA1
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
5from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6['pro', 'por', 'rpo', 'rop', 'opr', 'orp']3
Output:
ABC ACB BAC BCA CAB CBA
Hoán vị và kết hợp trong hoán vị Python của một chuỗi nhất định với các ký tự lặp lại ý tưởng là sử dụng từ điển để tránh in bản sao. & Nbsp;Permutations of a given string with repeating characters The idea is to use dictionary to avoid printing duplicates.
Python3
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
0 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
1from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
2 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
4 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
5from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
7from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
8from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
9['pro', 'por', 'rpo', 'rop', 'opr', 'orp']0
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']1
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6['pro', 'por', 'rpo', 'rop', 'opr', 'orp']3
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
8['pro', 'por', 'rpo', 'rop', 'opr', 'orp']5
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']6
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']7
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']8
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']9
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA0
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA1
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA2
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA3
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA4
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']030
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA7
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
6 ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']0
ABC ACB BAC BCA CAB CBA1
Hoán vị và kết hợp trong hoán vị Python của một chuỗi nhất định với các ký tự lặp lại ý tưởng là sử dụng từ điển để tránh in bản sao. & Nbsp;
yup
5yup
6
yup
5
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA1
yup
9Output:
GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG
2 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
yup
ypu
uyp
upy
puy
pyu
None
5 O[n!] where n is the size of the string.
Auxiliary Space: O[n!]