Thuật toán được triển khai trong mô -đun
import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
1 như là một phần của hàm import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
2:- Documentation;
- Mã nguồn.
def next_permutation[A]:
# Find the largest index i such that A[i] < A[i + 1]
for i in range[size - 2, -1, -1]:
if A[i] < A[i + 1]:
break
# If no such index exists, this permutation is the last one
else:
return
# Find the largest index j greater than j such that A[i] < A[j]
for j in range[size - 1, i, -1]:
if A[i] < A[j]:
break
# Swap the value of A[i] with that of A[j], then reverse the
# sequence from A[i + 1] to form the new permutation
A[i], A[j] = A[j], A[i]
A[i + 1 :] = A[: i - size : -1] # A[i + 1:][::-1]
Ngoài ra, nếu chuỗi được đảm bảo chỉ chứa các phần tử riêng biệt, thì
import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
3 có thể được thực hiện bằng cách sử dụng các chức năng từ mô -đun import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
1:import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
Trong bài đăng trên blog này, chúng tôi sẽ xem xét làm thế nào chúng tôi có thể tính toán hoán vị từ vựng tiếp theo của một danh sách. Chúng tôi sẽ thực hiện thuật toán trong Python 3.4.
1. Vấn đề chúng tôi được cung cấp một danh sách l số và chúng tôi muốn tìm hoán vị từ vựng tiếp theo của nó. Ví dụ, hãy để
5. Sau đó, hoán vị từ vựng tiếp theo là import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
6. Dưới đây là tất cả 24 hoán vị theo thứ tự từ vựng.import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
We are given a list L of numbers, and we want to find its next lexicographic permutation. For example, let
import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
5. Then the next lexicographic permutation is import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
6. Below are all 24 permutations in lexicographic order.[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
Làm thế nào chúng ta có thể tính toán chúng?
2. Giải pháp
2.1. Thuật toán Pandit, Wikipedia mô tả thuật toán sau của Narayana Pandit thay đổi danh sách tại chỗ để tạo ra hoán vị từ vựng tiếp theo.
Wikipedia describes the following algorithm by Narayana Pandit that changes the list in-place to generate the next lexicographic permutation.
- Tìm chỉ số lớn nhất
7 sao choimport more_itertools # raises IndexError if s is already the last permutation def next_permutation[s]: seq = sorted[s] n = more_itertools.permutation_index[s, seq] return more_itertools.nth_permutation[seq, len[seq], n+1]
8. Nếu không có chỉ số như vậy tồn tại, hoán vị là hoán vị cuối cùng.import more_itertools # raises IndexError if s is already the last permutation def next_permutation[s]: seq = sorted[s] n = more_itertools.permutation_index[s, seq] return more_itertools.nth_permutation[seq, len[seq], n+1]
- Tìm chỉ số lớn nhất
9 lớn hơnimport more_itertools # raises IndexError if s is already the last permutation def next_permutation[s]: seq = sorted[s] n = more_itertools.permutation_index[s, seq] return more_itertools.nth_permutation[seq, len[seq], n+1]
7 sao choimport more_itertools # raises IndexError if s is already the last permutation def next_permutation[s]: seq = sorted[s] n = more_itertools.permutation_index[s, seq] return more_itertools.nth_permutation[seq, len[seq], n+1]
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
1. - Trao đổi giá trị của
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
2 với giá trị của[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
3. - Đảo ngược trình tự từ
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
4 lên đến và bao gồm phần tử cuối cùng[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
5, trong đó l không được chỉ số bằng không.
2.2. Ví dụ Hãy để chúng tôi cố gắng hiểu thuật toán này với một ví dụ. Chúng tôi sẽ theo lời giải thích tuyệt vời này. Hãy xem xét danh sách [1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 1, 3]
[2, 4, 3, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 1, 2, 3]
[4, 1, 3, 2]
[4, 2, 1, 3]
[4, 2, 3, 1]
[4, 3, 1, 2]
[4, 3, 2, 1]
6. Ba hoán vị đầu tiên là
Let us try to understand this algorithm with an example. We will follow this excellent explanation.
Consider the list
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]6. The first three permutations are
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]
Chúng ta có thể thấy rằng ‘3, không thay đổi vị trí của nó trong quá trình chuyển đổi từ
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]7 sang
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]8. Tuy nhiên, trong quá trình chuyển đổi tiếp theo từ
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]8 sang
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]0 số 3 3 thay đổi vị trí của nó. Tại sao vậy?
Nếu bạn nhìn kỹ vào
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]8, bạn có thể thấy rằng tất cả các số ở bên phải của ‘3, theo thứ tự giảm dần. Điều này có nghĩa là không có hoán vị tiếp theo cho người phụ
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]2, vì vậy chúng tôi buộc phải làm điều gì đó với ‘3. Chính xác là, chúng tôi sẽ trao đổi ‘3 3 với một trong những con số trong trình sublist đó, cụ thể là với số cao hơn tiếp theo. Số cao hơn tiếp theo trong trình phụ trợ đó là ‘4, và bằng cách hoán đổi‘ 3, và 4, chúng tôi nhận được
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]3. ‘4 Hiện tại đang ở phía trước.
Tiếp theo chúng tôi muốn tạo ra các hoán vị với ‘4 ở phía trước, tuy nhiên, người phụ
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]4 ở bên phải của‘ 4, không phải là từ vựng nhỏ nhất cho các số 9, 3, 2, 1. Nó là mặt trái của atlist đó. Nếu chúng ta đảo ngược danh sách phụ đó, chúng ta sẽ nhận được
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]0, đó là hoán vị từ vựng nhỏ nhất với ‘4 ở phía trước. Cũng lưu ý rằng ở bên phải của ‘4, các số đang tăng lên. Và voilà, chúng tôi đã hoàn thành.
Đây chính xác là những gì các bước trong thuật toán Pandit từ làm. Dưới đây chúng được hiển thị chi tiết hơn.
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]
Do đó, chìa khóa để hiểu thuật toán là để nhận ra rằng hoán vị từ vựng lớn nhất của một người phụ được đạt được khi nó theo thứ tự giảm dần và hoán vị từ vựng nhỏ nhất của một người phụ được tạo ra khi các vật phẩm theo thứ tự tăng dần.
3. Thực hiện trong Python 3.4
def next_permutation[L]: ''' Permute the list L in-place to generate the next lexicographic permutation. Return True if such a permutation exists, else return False. ''' n = len[L] #------------------------------------------------------------ # Step 1: find rightmost position i such that L[i] < L[i+1] i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False #------------------------------------------------------------ # Step 2: find rightmost position j to the right of i such that L[j] > L[i] j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 #------------------------------------------------------------ # Step 3: swap L[i] and L[j] L[i], L[j] = L[j], L[i] #------------------------------------------------------------ # Step 4: reverse everything to the right of i left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True
Đây là một ví dụ:
def example[]: L = [1, 2, 3] while True: print[L] if not next_permutation[L]: break #---------------------------------------------------------------- if __name__ == "__main__": example[]
Đầu ra là:
[1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
Bài tập
1. Giải quyết vấn đề NextPerm trên spoj. Solve the problem NEXTPERM on SPOJ.
2 Print all the permutations of the string “000111” in lexicographic order.
3. Bạn được cho bộ [3, 9, 4, 1, 2]
[3, 9, 4, 2, 1]
[4, 1, 2, 3, 9]
6. Bây giờ, hãy xem xét tất cả các tập hợp con phần tử của S. có bao nhiêu tập hợp con có thuộc tính mà tổng các phần tử của chúng lớn hơn 20. Ví dụ, tổng của các phần tử trong tập hợp con [3, 9, 4, 1, 2]
[3, 9, 4, 2, 1]
[4, 1, 2, 3, 9]
7 lớn hơn 20, tuy nhiên tổng Trong số các yếu tố trong tập hợp con [3, 9, 4, 1, 2]
[3, 9, 4, 2, 1]
[4, 1, 2, 3, 9]
8 không lớn hơn 20. You are given the set
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]6. Now, consider all 4-element subsets of S. How many of those subsets have the property that the sum of their elements is greater than 20. For example, the sum of the elements in the subset
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]7 is greater than 20, however the sum of the elements in the subset
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]8 is not greater than 20.
4. Giải các vấn đề ở trên bằng cách sử dụng hàm tích hợp std :: next_permuting trong C ++. Solve the problems above by using the built-in function std::next_permutation in C++.
Giải pháp cho bài tập 3 Dưới đây là ba phương pháp để giải quyết vấn đề này. Tất cả đều trả về kết quả [3, 9, 4, 1, 2]
[3, 9, 4, 2, 1]
[4, 1, 2, 3, 9]
9. Bộ được lưu trữ dưới dạng danh sách L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
0.
Here are three methods to solve this problem. All of them return the result
[3, 9, 4, 1, 2] [3, 9, 4, 2, 1] [4, 1, 2, 3, 9]9. The set is stored as a list
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]0.
Phương pháp 1 Chúng tôi sử dụng bốn vòng lặp để mô phỏng bốn con trỏ L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
1 và L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
2 di chuyển qua L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
3 và chúng tôi tính toán tổng L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
4.
We use four for-loops to simulate four pointers
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]1 and
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]2 that move over
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]3, and we calculate the sum
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]4.
def method1[]: L = [2, 3, 5, 7, 11, 13, 17] n = len[L] cnt = 0 for i in range[n-3]: for j in range[i+1, n-2]: for k in range[j+1, n-1]: for m in range[k+1, n]: if L[i] + L[j] + L[k] + L[m] > 20: cnt += 1 return cnt
def method2[]: L = [2, 3, 5, 7, 11, 13, 17] cnt = 0 from itertools import combinations for subset in combinations[L, 4]: if sum[subset] > 20: cnt += 1 return cnt
Phương pháp 3 cuối cùng chúng tôi sử dụng chức năng của mình
3. Bí quyết là tạo ra tất cả các hoán vị của danh sách import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
6 đóng vai trò là bitmasks. Một Bitmask cho chúng tôi biết những mục nào trong L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
3 chúng tôi chọn, cụ thể là những thứ mà Bitmask được đặt thành L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
8. Ví dụ: Bitmask L = [3, 9, 4, 2, 1]
__Step 1: find rightmost position i such that L[i] < L[i+1]
Result: i = 0 and L[i] = 3
__Step 2: find rightmost position j to the right of i such that L[j] > L[i]
Result: j = 2 and L[j] = 4
__Step 3: swap L[i] and L[j]
Swappping at indices: 0 and 2
Result after swapping: [4, 9, 3, 2, 1]
__Step 4: reverse everything to the right of i
Result of reversing: [4, 1, 2, 3, 9]
9 bảo chúng tôi chọn các mục tại các vị trí def next_permutation[L]:
'''
Permute the list L in-place to generate the next lexicographic permutation.
Return True if such a permutation exists, else return False.
'''
n = len[L]
#------------------------------------------------------------
# Step 1: find rightmost position i such that L[i] < L[i+1]
i = n - 2
while i >= 0 and L[i] >= L[i+1]:
i -= 1
if i == -1:
return False
#------------------------------------------------------------
# Step 2: find rightmost position j to the right of i such that L[j] > L[i]
j = i + 1
while j < n and L[j] > L[i]:
j += 1
j -= 1
#------------------------------------------------------------
# Step 3: swap L[i] and L[j]
L[i], L[j] = L[j], L[i]
#------------------------------------------------------------
# Step 4: reverse everything to the right of i
left = i + 1
right = n - 1
while left < right:
L[left], L[right] = L[right], L[left]
left += 1
right -= 1
return True
0 và def next_permutation[L]:
'''
Permute the list L in-place to generate the next lexicographic permutation.
Return True if such a permutation exists, else return False.
'''
n = len[L]
#------------------------------------------------------------
# Step 1: find rightmost position i such that L[i] < L[i+1]
i = n - 2
while i >= 0 and L[i] >= L[i+1]:
i -= 1
if i == -1:
return False
#------------------------------------------------------------
# Step 2: find rightmost position j to the right of i such that L[j] > L[i]
j = i + 1
while j < n and L[j] > L[i]:
j += 1
j -= 1
#------------------------------------------------------------
# Step 3: swap L[i] and L[j]
L[i], L[j] = L[j], L[i]
#------------------------------------------------------------
# Step 4: reverse everything to the right of i
left = i + 1
right = n - 1
while left < right:
L[left], L[right] = L[right], L[left]
left += 1
right -= 1
return True
1. Xem thêm Bài tập 2.
We finally use our function
import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
3. The trick is to generate all permutations of the list L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]6 which serve as bitmasks. A bitmask tells us which items in
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]3 we pick, namely those ones where the bitmask is set to
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]8. For example, the bitmask
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]9 tells us to pick the items at positions
def next_permutation[L]: ''' Permute the list L in-place to generate the next lexicographic permutation. Return True if such a permutation exists, else return False. ''' n = len[L] #------------------------------------------------------------ # Step 1: find rightmost position i such that L[i] < L[i+1] i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False #------------------------------------------------------------ # Step 2: find rightmost position j to the right of i such that L[j] > L[i] j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 #------------------------------------------------------------ # Step 3: swap L[i] and L[j] L[i], L[j] = L[j], L[i] #------------------------------------------------------------ # Step 4: reverse everything to the right of i left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True0 and
def next_permutation[L]: ''' Permute the list L in-place to generate the next lexicographic permutation. Return True if such a permutation exists, else return False. ''' n = len[L] #------------------------------------------------------------ # Step 1: find rightmost position i such that L[i] < L[i+1] i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False #------------------------------------------------------------ # Step 2: find rightmost position j to the right of i such that L[j] > L[i] j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 #------------------------------------------------------------ # Step 3: swap L[i] and L[j] L[i], L[j] = L[j], L[i] #------------------------------------------------------------ # Step 4: reverse everything to the right of i left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True1. See also exercise 2.
import more_itertools
# raises IndexError if s is already the last permutation
def next_permutation[s]:
seq = sorted[s]
n = more_itertools.permutation_index[s, seq]
return more_itertools.nth_permutation[seq, len[seq], n+1]
0Để tính tổng các phần tử trong tập hợp con, chúng tôi tính toán sản phẩm DOT giữa
L = [3, 9, 4, 2, 1] __Step 1: find rightmost position i such that L[i] < L[i+1] Result: i = 0 and L[i] = 3 __Step 2: find rightmost position j to the right of i such that L[j] > L[i] Result: j = 2 and L[j] = 4 __Step 3: swap L[i] and L[j] Swappping at indices: 0 and 2 Result after swapping: [4, 9, 3, 2, 1] __Step 4: reverse everything to the right of i Result of reversing: [4, 1, 2, 3, 9]3 và
def next_permutation[L]: ''' Permute the list L in-place to generate the next lexicographic permutation. Return True if such a permutation exists, else return False. ''' n = len[L] #------------------------------------------------------------ # Step 1: find rightmost position i such that L[i] < L[i+1] i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False #------------------------------------------------------------ # Step 2: find rightmost position j to the right of i such that L[j] > L[i] j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 #------------------------------------------------------------ # Step 3: swap L[i] and L[j] L[i], L[j] = L[j], L[i] #------------------------------------------------------------ # Step 4: reverse everything to the right of i left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True3 qua
def next_permutation[L]: ''' Permute the list L in-place to generate the next lexicographic permutation. Return True if such a permutation exists, else return False. ''' n = len[L] #------------------------------------------------------------ # Step 1: find rightmost position i such that L[i] < L[i+1] i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False #------------------------------------------------------------ # Step 2: find rightmost position j to the right of i such that L[j] > L[i] j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 #------------------------------------------------------------ # Step 3: swap L[i] and L[j] L[i], L[j] = L[j], L[i] #------------------------------------------------------------ # Step 4: reverse everything to the right of i left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True4