Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This is suspicious:
foreach my $i ($#$arr) { $avg = ($i + ($avg ? $avg : $i)) / 2; }

This only runs once, with $i taking the value of the last index of $arr. You probably meant @$arr.

The next problem is that this isn't an average in the normal sense, it's a weighted average where the last element gets much more weight than the first, more like:

$avg = (...(($arr->[0]/2 + $arr->[1])/2 + $arr->[2])/2 + ... $arr->[$# +$arr])/2;

So perhaps you meant a decaying average, but it's not clear from the context. Here's something equivalent, with output:

my @x = (10, 7, 5, 10, 50, 70, 75, 72, 79, 80); sub avg { my @arr = @_; my $avg; foreach my $i (@arr) { print "$i: $avg\n"; $avg = ($i + ($avg ? $avg : $i))/2; } return $avg; } print avg(@x),"\n"; 10: # undef 7: 10 5: 8.5 10: 6.75 50: 8.375 70: 29.1875 75: 49.59375 72: 62.296875 79: 67.1484375 80: 73.07421875 76.537109375 print avg(reverse @x),"\n"; 80: # undef 79: 80 72: 79.5 75: 75.75 70: 75.375 50: 72.6875 10: 61.34375 5: 35.671875 7: 20.3359375 10: 13.66796875 11.833984375

Making a change to produce the uniformly weighted average:

my @x = (10, 7, 5, 10, 50, 70, 75, 72, 79, 80); sub avg2 { my @arr = @_; my $sum; foreach my $i (@arr) { print "$i: $sum\n"; $sum += $i; } return $sum/@arr; } print avg(@x),"\n"; 10: # undef 7: 10 5: 17 10: 22 50: 32 70: 82 75: 152 72: 227 79: 299 80: 378 45.8 print avg(reverse @x),"\n"; 80: # undef 79: 80 72: 159 75: 231 70: 306 50: 376 10: 426 5: 436 7: 441 10: 448 45.8

where the forward and reverse lists produce the same average.

A lot of the syntax would be less cumbersome if you started with my @arr = (...).

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: grouping numbers by QM
in thread grouping numbers by ag4ve

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-07-19 00:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (398 votes). Check out past polls.

      Notices?