Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem with your algorithm is your determination of <code>loc_pi</code>. For example, consider the case where 1000 is the first <code>pi</code> chosen in <code>loc_pi=seq.index(pi)</code>. In that case, <code>loc_pi</code> will equal 2 since 1000 is at index 2 of the sequence, and the function will return 1000, which we know is absolutely not order statistic 2.</p> <p>Thus, we know we can't determine <code>loc_pi</code> based on the index of the randomly chosen <code>pi</code>. After all, that list is in an arbitrary order- its position means nothing. What you are actually trying to get for that <code>loc_pi</code> value is <em>the number of elements in that sublist that are below your chosen pi</em>. And thankfully, that is easy to get! Just change the line:</p> <pre><code> return lo,pi,hi,loc_pi </code></pre> <p>to</p> <pre><code> return lo,pi,hi,len(lo) + 1 </code></pre> <p>And you'll find it performs correctly and consistently!</p> <pre><code>dynamic-oit-vapornet-c-913:test dgrtwo$ python test21.py Sequence [54, -1, 1000, 565, 64, 2, 5] Statistic pi 565 Location 3 pi 5 Location 5 pi -1 Location 0 pi 2 Location 0 2 dynamic-oit-vapornet-c-913:test dgrtwo$ python test21.py Sequence [54, -1, 1000, 565, 64, 2, 5] Statistic pi -1 Location 1 pi 54 Location 0 pi 5 Location 2 pi 2 Location 0 2 </code></pre> <p>ETA: Please also note that your algorithm as written will <em>not</em> always work if there are ties in the input sequence. Try a few examples and you will see what I mean. There are simple ways to solve it that I am sure you can figure out.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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