Keep It Simple, Stupid PerlMonks

Re: grouping numbers

by QM (Parson)
 on Jul 11, 2013 at 14:31 UTC ( #1043738=note: 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

Create A New User
Node Status?
node history
Node Type: note [id://1043738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-10-22 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I need money for a bigger acquisition, I usually ...

Results (122 votes). Check out past polls.

Notices?