Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Any improvement over the code possible??

by Kenosis (Priest)
on Sep 21, 2012 at 18:00 UTC ( #994961=note: print w/ replies, xml ) Need Help??


in reply to Any improvement over the code possible??

Consider the following:

sub find_above_average { my $mean = &calculate_average(@_); return grep $_ > $mean, @_; }

Remove the say line and print the average outside of the subroutine, and use grep to return only those values greater than the value of $mean.

sub calculate_average { @_ > 0 or die 'No numbers sent for averaging.'; my $sum = 0; foreach (@_) { $sum += $_; } return $sum / @_; }

Check for an empty list at the top to avoid a division by zero error.


Comment on Re: Any improvement over the code possible??
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994961]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (18)
As of 2015-07-28 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls