64 506
Assignments Done
99,1%
Successfully Done
In September 2018

Answer to Question #15325 in Perl for Andrew

Question #15325
How to create a perl program that finds the distance between two points in an array?
Expert's answer
#!/usr/bin/perl
use strict; use warnings;
use List::Util qw( min );
use Statistics::Descriptive;

my $stat = Statistics::Descriptive::Full->new;
my @long = (100, 200, 210, 300, 350, 400, 401, 402, 403, 404, 405, 406);
my @short = (3, 6, 120, 190, 208, 210, 300, 350);

for my $x ( @short ) {
$stat->add_data(find_dist($x, @long));
}

my $freq = $stat->frequency_distribution_ref([0, 2, 10, 20, 94, 97]);
for my $bin ( sort { $a <=> $b } keys %$freq ) {
print "$bin: $freq->{$bin}
";
}

sub find_dist {
my ($x, $v) = @_;
return min map abs($x - $_), @$v;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions