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
,
yup ypu uyp upy puy pyu None1,
yup ypu uyp upy puy pyu None2,
yup ypu uyp upy puy pyu None3,
yup ypu uyp upy puy pyu None4,
yup ypu uyp upy puy pyu None5 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
7 đượ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
8. - Trong mỗi lần lặp của vòng lặp, mỗi ký tự của
yup
được lưu trữ trong
0.from itertools import permutations words = [''.join[p] for p in permutations['pro']] print[words]
- 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
Đọc
Method #1: Using Naive Method
Python3
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
2from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
4from 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]
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]
8GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG8
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 yup
0Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']6
yup
2Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
yup
4Xem thảo luận
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']0
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']1
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']4
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']3
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']4
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']0
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']6
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']7
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']9
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']0
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']1
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']223
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']4
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']1
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']6
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']7
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']8
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']9
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']1
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']223
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA3
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA4
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA5
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA6
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA7
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA9
ABC ACB BAC BCA CAB CBA0
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]
6ABC ACB BAC BCA CAB CBA3
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
ABC ACB BAC BCA CAB CBA5
ABC ACB BAC BCA CAB CBA6
Output:
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
& nbsp; & nbsp; Phương thức #2: Sử dụng itertools & nbsp; & nbsp;
Method #2: Using itertools
Python3
ABC ACB BAC BCA CAB CBA7
ABC ACB BAC BCA CAB CBA8
ABC ACB BAC BCA CAB CBA9
GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG0
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
2from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
4from 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]
8GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG8
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 yup
0Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']6
yup
2Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
yup
4from 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]
6yup
7Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA8
ABC ACB BAC BCA CAB CBA5
yup ypu uyp upy puy pyu None00
Output:
Initial string abc Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
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
Đọc
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA
Bàn luận
Python3
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ụ:
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;
ABC ACB BAC BCA CAB CBA7
ABC ACB BAC BCA CAB CBA8
ABC ACB BAC BCA CAB CBA9
GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG0
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']2
yup ypu uyp upy puy pyu None06
ABC ACB BAC BCA CAB CBA55____108
‘
yup ypu uyp upy puy pyu None09
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']6
yup ypu uyp upy puy pyu None17
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA5
yup ypu uyp upy puy pyu None20
yup ypu uyp upy puy pyu None21
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
5 yup ypu uyp upy puy pyu None23
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']4
yup ypu uyp upy puy pyu None06
ABC ACB BAC BCA CAB CBA5
yup ypu uyp upy puy pyu None14
Output:
ABC ACB BAC BCA CAB CBA
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']5
yup ypu uyp upy puy pyu None25
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]
3 yup ypu uyp upy puy pyu None28Permutations of a given string with repeating characters The idea is to use dictionary to avoid printing duplicates.
Python3
ABC ACB BAC BCA CAB CBA7
ABC ACB BAC BCA CAB CBA8
ABC ACB BAC BCA CAB CBA9
GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG0
ABC ACB BAC BCA CAB CBA9
yup ypu uyp upy puy pyu None7
yup ypu uyp upy puy pyu None43
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 yup ypu uyp upy puy pyu None45
yup ypu uyp upy puy pyu None46
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 yup ypu uyp upy puy pyu None48
yup
2
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 yup ypu uyp upy puy pyu None51
yup ypu uyp upy puy pyu None52
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
3 ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']1
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']6
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']6
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA5
yup ypu uyp upy puy pyu None59
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']4
['pro', 'por', 'rpo', 'rop', 'opr', 'orp']5
yup ypu uyp upy puy pyu None62
yup ypu uyp upy puy pyu None63
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']8
yup ypu uyp upy puy pyu None65
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']0
yup ypu uyp upy puy pyu None67
Initial string abc Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']0
from itertools import permutations
words = [''.join[p] for p in permutations['pro']]
print[words]
5yup ypu uyp upy puy pyu None70
Output:
GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG
Độ phức tạp về thời gian: O [n!] Trong đó n là kích thước của chuỗi.AUXILIARY: O [n!] & NBSP; O[n!] where n is the size of the string.
Auxiliary Space: O[n!]