Hướng dẫn how do you generate the next permutation in python? - làm cách nào để tạo hoán vị tiếp theo trong python?
Thuật toán được triển khai trong mô -đun 1 như là một phần của hàm 2: Show
Ngoài ra, nếu chuỗi được đảm bảo chỉ chứa các phần tử riêng biệt, thì 3 có thể được thực hiện bằng cách sử dụng các chức năng từ mô -đun 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. Then the next lexicographic permutation is 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.
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. 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. 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. 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 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. 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. 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 Làm thế nào để bạn tạo ra tất cả các hoán vị của một chuỗi trong Python?Để tìm tất cả các hoán vị có thể của một chuỗi đã cho, bạn có thể sử dụng mô -đun ITERTOOLS có một phương thức hữu ích gọi là hoán vị (Itable [, R]).Phương pháp này trả về hoán vị chiều dài r liên tiếp của các phần tử trong các bộ dữ liệu có thể lặp lại.use the itertools module which has a useful method called permutations(iterable[, r]). This method return successive r length permutations of elements in the iterable as tuples.
Chức năng hoán vị tiếp theo là gì?Thuật toán C ++ Hàm next_permuting () được sử dụng để sắp xếp lại các phần tử trong phạm vi [đầu tiên, cuối cùng) vào hoán vị từ vựng từ vựng tiếp theo.Một hoán vị được chỉ định là từng cách có thể trong đó một tập hợp hoặc số lượng thứ có thể được đặt hàng hoặc sắp xếp.Nó được ký hiệu là N!used to reorder the elements in the range [first, last) into the next lexicographically greater permutation. A permutation is specified as each of several possible ways in which a set or number of things can be ordered or arranged. It is denoted as N!
Làm thế nào để bạn tìm thấy giá trị hoán vị trong Python?Để tính toán các hoán vị trong Python, hãy sử dụng phương thức itertools.permut ().Các itertools.Phương thức hoán vị () lấy một danh sách, từ điển, tuple hoặc các trình lặp khác làm tham số và trả về các hoán vị của danh sách đó.use the itertools. permutation() method. The itertools. permutations() method takes a list, dictionary, tuple, or other iterators as a parameter and returns the permutations of that list. |