![]() ![]() ![]() ![]() If (depth = 0) // using introspection we now know that we selected bad pivots. If (size A != size B) // different size so not equal Using a kind of metode IntroSort a variant of QuickSort to quickly find out if they are equal.ĭepth is 2 times the Log2(size A). If you have large data structures and small keys you want to compare then make a new list with just the values you want to compare. This might be totally over engineered compared to what you need.ĭepending on what you input data actually is there are fast or faster metodes to find the difference.Īll tests should start with a size comparison as that is a fast way to find out that they are not equal. Now compare_lists takes a variable number of arguments, and would return True if all of the arguments are permutations of each other. Return all(first_counter = counter for counter in counters) Now that you have an understanding of what it means to generate all combinations of a string in Python, let’s see how we can easily generate the different permutations of a string. If you wanted to make this function scalable to an arbitrary number of lists, you could do: from collections import Counter So, in our example of 'abc', we would calculate the value of 3, which actually evaluates to 3x2x1 6. Constructing the sorted list has O(n) space complexity, while constructing the Counter is O(k), where k is the number of unique elements. Call itertools.permutations(iterable, r) with the longer list as iterable and the length of the shorter list as r to return all r-length permutations of the. There is also a more efficient memory allocation involved if you do not want to mutate list1 and list2. This is also an improvement over the solution in the question, which is O(n ^ 2). While list.sort / sorted has O(n log n) time complexity, constructing a Counter (which is a dict internally) is O(n). You can also do: from collections import Counter ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |