Note that there are some explanatory texts on larger screens.

plurals
  1. PORearranging a sequence to maximize difference in order, ruby
    primarykey
    data
    text
    <p>I have an ordered sequence in the form of an array. e.g.</p> <pre><code>original_order = [1,2,3,4,5,6,7,8,9,10] </code></pre> <p>I want to know how to re-order the sequence, with the maximum difference in order, with regard to the original order. Specifically, I want to know what order would give the Maximum "score", when running the code below:</p> <p>Put simply, the below calculates the distance that each value from the sequence has "moved" in the rearranged sequence (array called rearranged), when compared to its position in the original_order sequence. It then sums the differences (distances moved) for each value, to give an overall score of "difference in order" for the rearrangement. Someone told me that this "score" I am calculating can be defined as the ordinal similarity between the two sequences (original order and rearranged).</p> <p>I think the max score can be obtained by running the code with the rearranged order as the <strong>reversed</strong> original order. I have not found a method of rearrangement that has a higher score than this, though If anyone thinks I am wrong please let me know (for the example original_order above this would be max_score = 50). I think that no other rearrangement of the sequence order can give a higher score (although there are other orders that give the same score).</p> <pre><code>position = original_order.map{|x| rearranged.index(x)} #works out the index of original_order values in rearranged index_values = Array(0..(original_order.length - 1)) # array of index values for original_order both = [] both &lt;&lt; position both &lt;&lt; index_values difference = both.transpose.map {|x| x.reduce(:-)} # taking away old position from new position, to find the distance that the value has "moved" when re-ordered difference_abs = [] difference.each do |i| difference_abs &lt;&lt; i.abs end score = difference_abs.inject(:+) </code></pre> <p>What I would like to know is:</p> <ol> <li><p>if my hypothesis that the reversed order will always give the highest possible score, regardless of the length of the sequence array, is correct</p></li> <li><p>a mathematical explanation of what my code does, and why simply reversing the original order gives the highest possible score</p></li> <li><p>if ordinal similarity is an accurate way of defining my score metric, and if not, what is?</p></li> </ol> <p>Also, any tips on simplifying my code are welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload