r/dailyprogrammer • u/Godspiral 3 3 • May 06 '16
[2016-05-04] Challenge #265 [Hard] Permutations with repeat
The number of permutations of a list that includes repeats is `(factorial of list length) / (product of factorials of each items repeat frequency)
for the list 0 0 1 2 the permutations in order are
0 0 1 2
0 0 2 1
0 1 0 2
0 1 2 0
0 2 0 1
0 2 1 0
1 0 0 2
1 0 2 0
1 2 0 0
2 0 0 1
2 0 1 0
2 1 0 0
1. Calculate permutation number of list that may include repeats
The permutation number is similar to Monday and Wednesday's challenge. But only wednesday's approach of calculating it without generating the full list will work (fast) for the longer inputs. The input varies from previous ones in that you are provided a list rather than a number to account for possible repeats. If there are no repeats, then the answer is the same as the part 2 (wednesday) challenge.
input:
5 4 3 2 1 0
2 1 0 0
5 0 1 2 5 0 1 2 0 0 1 1 5 4 3 2 1 0
8 8 8 8 8 8 8 8 8 7 7 7 6 5 0 1 2 5 0 1 2 0 0 1 1 5 4 3 2 1 0 6 7 8
output: (0 based indexes)
719
11
10577286119
3269605362042919527837624
2. retrieve list from permutation number and sorted list
input is in format: permutation_number, sorted list to permute
output format is above part 1 input rows.
input:
719, 0 1 2 3 4 5
11, 0 0 1 2
10577286119, 0 0 0 0 0 1 1 1 1 1 2 2 2 3 4 5 5 5
3269605362042919527837624, 0 0 0 0 0 1 1 1 1 1 2 2 2 3 4 5 5 5 6 6 7 7 7 7 8 8 8 8 8 8 8 8 8 8
bonus
use the above function and wednesday's combination number (optional) to compress/encode a list into a fixed set of numbers (with enough information to decode it)
input:
hello, heely owler world!
You might wish to convert to ascii, then calculate the combination number for the unique ascii codes, then calculate the permutation number with each letter replaced by contiguous indexes.
2
u/Gobbedyret 1 0 May 07 '16
Python 3.5 with no bonus.
I used a refinement of the same approach I took to the last two challenges. Importantly, these are implemented iteratively, not recursively. This should give a speed increase as well as permitting analysis of lists larger than the maximal recursion depth.
It's not particularly easy to read, but the logic is straightforward. To find the first digit, it calculates for each of the candidate digits what permutation number would correspond to placing that digit there, using the simple math described in the problem description.
This is really not how one should write Python. Python's supposed to be short and readable. But I couldn't resist optimizing.
Speed:
Finding permutation number of shuffled list of 10 copies of 100 elements each: 36.0 ms
Finding 101000th permutation of shuffled list of 10 copies of 100 elements each: 26.1 ms
3
u/Godspiral 3 3 May 06 '16 edited May 06 '16
in J, recursive
undo with permNF and compare with original
1
u/inad156 May 10 '16
First post here. I'm a bit confused about part 2. If the input is a sorted list, how would you go about trying to figure out what it's supposed to be?
Python 2.7
3
u/gabyjunior 1 2 May 06 '16 edited May 07 '16
BC script
Both challenges included, for the bonus I am only checking that the conversion is working both ways once the text is converted to ascii.
Still need to figure out how to compress the sorted list and the ascii codes/indexes map.EDIT added compress/uncompress, the compressed data includes 6 numbers:
New source code here.
Output
2
u/[deleted] May 07 '16 edited May 08 '16
perl... this does both finding the permutation number of a scrambled list, and recreating the list from the number. It's a fast, non-recursive solution.
+/u/CompileBot perl --time
Input:
2
u/ExSTATStic May 08 '16 edited May 09 '16
As before, I'm super new to c/cython and I should really read up a bit more, but cythonizing is super forgiving so I'm just plodding ahead.
Here's a cython version
I will implement the other parts in a little bit
Edit: Here's the second part
Edit2: Bonus was done without changing the code, here's the [executable python code] (https://gist.github.com/ctm22396/64eaa394c80ecc847d319d2609f49c55)
and the output