Free Trial

Cloud Quests

Elevate your workflow skills by solving real-world challenges using the Alteryx Analytics Cloud Platform.

Cloud Quest #32: Pony Permutations

Qiu
21 - Polaris
21 - Polaris

@CoG 
This is briliant. I did not even know the Formula "Factorial". Thanks.

Why your Handle name icon not yet with Alteryx? 😂

PangHC
12 - Quasar

@CoG amazing. but i not understand how ceil + mod is working. Did you refer to any article?

btw, it seems like not support more than 10? because the regex_replace is  ",?"

RWvanLeeuwen
11 - Bolide

thanks for sharing @CoG 

patrick_digan
17 - Castor
17 - Castor
Spoiler
image.png
CoG
14 - Magnetar

@PangHC 

I did not refer to any other sources. The application of the logic is entirely my own. This should support as many values as your system has memory and you have time. the ",?" is just to keep track of commas and remove them when they are present but protect against the Regex failing if the last element is removed since there isn't a comma at the end of the list.

 

As far as approach, this is a multi-pass process. You can think of any permutation of n items as a unique permutation of n-1 items with the nth being inserted to complete the list. It's like building a bitmask, but instead we build a permutation mask. So in the case of 4 ponies, there are 4! = 24 possible orderings, which is also 4 * (3!). So the ceil + mod identify which place each value will occupy. For example (this isn't quite the order the workflow does it, but it's easier to explain this way), the first six records correspond to 4,a,b,c (where abc are arbitrary), the next six records correspond to a,4,b,c; and so on. This is why we use the Multi-Row Formula Tool to perform successive division. Notice in the prior examples if we remove the '4', we are left with a,b,c and so we can perform the same logic with the 3rd place pony (3,a,b | a,3,b | a,b,3), and so on until we have one element left. By the end of this ceil + mod process we have a list of numbers that uniquely defines a permutation, by describing the index of that element in successively smaller sets:

Take the following set 3,1,0,0; and we will start with the unknown place permutation a, b, c, d

  1. The fourth pony is in index 3, so we have a, b, c, 4
  2. Of the places that still remain a,b,c, the third pony is in index 1, so we have a, 3, b, 4
  3. The second pony is in index 0, so we have 2, 3, b, 4
  4. And finally, the first pony is in index 0 (all that's left), giving us the final placement permutation: 2, 3, 1, 4

Hopefully, this helps clear things up a little. Reading over it, I feel like a better explanation exists, but I'm not sure how to provide it.