Hmm. That reads like an exam question. None the less, since it was interesting to code, here's how to do section d):

#! perl -sw use strict; use List::Util qw[ min ]; use Data::Dump qw[ pp ]; my %pvalues = ( 1=> 0.5453980, 2=> 0.4902384, 3=> 0.8167950, 4=> 0.2821822, 5=> 0.4693030, 6=> 0.6491767, 7=> 0.9802138, 8=> 0.1155778, 9=> 0.9585124, 10=> 0.4069490 ); my @orderedKeys = sort { $pvalues{ $b } <=> $pvalues{ $a } } keys %pvalues; my $d = my $n = values %pvalues; $pvalues{ $_ } *= $n / $d-- for @orderedKeys; $pvalues{ $orderedKeys[ $_ ] } = min( @pvalues{ @orderedKeys[ 0 .. $_ ] } ) for 1 .. $n-1; pp \%pvalues; __END__ c:\test>junk68 { 1 => "0.908996666666667", 2 => "0.908996666666667", 3 => "0.9802138", 4 => "0.908996666666667", 5 => "0.908996666666667", 6 => "0.927395285714286", 7 => "0.9802138", 8 => "0.908996666666667", 9 => "0.9802138", 10 => "0.908996666666667", }

I haven't done the last step, (I can't see the stuff in red), so you'll have to work out how to do that yourself. And to do that, you'll first need to understand how the new line above works. And if you can do that, you'll stand some chance of explaining it to whomever is going to check your work.

That'll be $25 :)

BTW:If this is going to be used for real on large volumes of data (which R code often is), then you'll want to replace the use of List::Utilmin() with a custom min() that doesn't use a list for input. Throwing large lists around is a sure-fire way to kill performance. That said, if the lists are large, then all the nested slicing is going to kill you anyway.

For real performance you might consider recoding this for PDL, but that's definitely left as an exercise.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

In reply to Re^5: Restore the original order of an array after sort and performing some funchtion on array values by BrowserUk
in thread Restore the original order of an array after sort and performing some funchtion on array values by sesemin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.