This shuffle method actually shuffles the element in place meaning it modifies the original list, hence, the ordering of the original elements in the List is lost. The random.shuffle() is a commonly used and recommended method to shuffle a list in Python. Python Shuffle List using random.shuffle() Function # Using random.randint() and pop() functionĢ. Permutations = list(itertools.permutations(mylist)) # Example 4: Using itertools.permutations() function # Example 3: Using Fisher–Yates shuffle algorithm Yield from enum1(, count, sum(count.Result = random.sample(mylist, len(mylist)) Yield from enum2(prefix, x, count, total, mode) Yield from enum2(prefix, mode, count, total, mode)ĭefect_okay = not prefix or count] * 2 > total Yield from enum1(prefix, count, total - 1, mode) Return max(ems(), key=itemgetter(1))ĭef enum2(prefix, x, count, total, mode): The worst-case delay unfortunately is quadratic it could be reduced to linear (optimal) with better data structures. The asymptotic running time is optimal, since the overhead of generation is on the order of the length of the output. This is tobias_k's answer plus efficient tests to detect when the choice currently under consideration is globally constrained in some way. In doing so, we introduce at most one defect (where x used to be) and eliminate one defect (the yy). Modify Q by moving the first occurrence of x to the front. In both cases, we find an optimal solution T that extends P', as needed. If the first substring has more y's than x's, then some other substring has more x's than y's, and we can rewrite these substrings without additional defects so that x goes first. If the first substring has at least as many x's as y's, then it can be rewritten without introducing additional defects to begin with x. Consider the maximal substrings of Q containing only x and y. By the algorithm's choice, x is at least as frequent in Q as y. Suppose first that P does not end with y. Otherwise, let P' = Px and S = PQ and Q = yQ', where x and y are items and Q and Q' are sequences. Otherwise, let P be the (safe) prefix just before the step under consideration, let P' be the prefix just after, and let S be an optimal solution extending P. The only way that a greedy step introduces a defect is if only one item type remains, in which case there is only one way to continue, and that way is safe. It suffices to show inductively that each greedy step maintains safety. Clearly the empty prefix is safe, and if a safe prefix is a whole solution then that solution is optimal. We call a prefix (partial solution) safe if it extends to an optimal solution. This greedy algorithm returns optimal solutions, by the following logic. The others are symmetric each instance of the minority element prevents at most two defects out of a total of k1 + k2 - 1 possible. Heapq.heappush(heap, (minuscount1, key1))įor two item types, with counts k1 and k2, the optimal solution has k2 - k1 - 1 defects if k1 k2. Heapq.heappush(heap, (minuscount2, key2)) (See also Coady's implementation of this algorithm.) import collections The idea is to take the most frequent of the remaining item types unless it was just taken. This is along the lines of Thijser's currently incomplete pseudocode. Additional points to David for the correctness proof. and David also answered the bonus question (generate all permutations). Eisenstat, and provided functions that generate the best possible permutation flawlessly. UPD: Choosing a winner here was a tough choice, because many people posted excellent answers. This is the code I'm using to test the solutions: Since the lists are large, generating and filtering all permutations is not an option.īonus question: how to generate all such permutations efficiently? More formally, given a list a, generate a permutation p of it that minimizes the number of pairs p=p. How can I achieve the opposite task - shuffle the list so that equal elements are never (or as seldom as possible) adjacent?įor example, for the above list one of the possible solutions is p = When we sort a list, like a = Įqual elements are always adjacent in the resulting list.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |