Similarly, we’ll call the orientations of the books u and d. We can define them as the characters in a string. To make things fit here in the post, I’m going to reduce the size of the problem to three books, which we’ll call A, B, and C. 2 python:įrom itertools import permutations, product We’ll be using the itertools permutations and product functions. You have to step (or iterate) through an iterator to get its contents. Printing an iterator object gets you a description like this, These are Python objects that represent a stream of data, but which don’t provide the entire stream at once. The first thing I learned was that the itertools library is, as its name implies, all about iterators. And so there is I just had to learn how to use it. My first thought was to write a recursive function or two, but I stopped myself, figuring there must be a Python library that’s already solved this problem. The harder part is listing all the arrangements. The second is the up-or-down sequence of the six books, which can be done in \(2^6 = 64\) different ways.īecause each of the 720 orderings of the books can have 64 up/down orientation sequences, the total number of arrangements of this type is \(720\times64=46,080\). Six items can be ordered in \(6! = 720\) different ways. The first is the ordering of the books, which is a permutation problem. The key to solving this problem is recognizing that it’s essentially an overlay of two simple problems. Other manipulations of the books, like spinning them around or setting them on their spine, are not considered. Here are a couple of example arrangements. How many ways can you arrange the books, considering both the left-to-right order of the books and whether they’re set with the front cover facing up or down? Six books are lying on a table in front of you. I solved the math portion of the problem (how many possibilities are there?) with a couple of elementary calculations, and I solved the enumeration portion of the problem (list all the possibilities) by learning how to use a couple of functions in the Python itertools library. Yesterday I ran into an interesting little two-stage combinatorics problem. If you want to restore the original order, use sorted(set(perm)), since permutations returns in lexicographical order (if your original string was in sorted order).Next post Previous post Combinatorics and itertools This may result in a different order to the printout. You could do this: from itertools import permutations (Perhaps call it multinomial though that word refers to a number, not the actual lists.) An easier way, perhaps slower in execution and memory usage but much faster in programming, is to use permutations and Python's set to remove the duplicates. You could get what you want by coding your own equivalent function to permutations but that would take a while to code and debug. The number of the results you want is called the multinomial coefficient for 4 values, 2 equal and 2 others equal. This means that though the two As look equal to you, itertools treats them as if they are not equal, since they have different positions in the original string. The documentation for permutations() states:Įlements are treated as unique based on their position, not on their value. There is no direct way to do that in itertools.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |