Skip to content

Instantly share code, notes, and snippets.

@dayfuaim
Last active November 1, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayfuaim/bea586bacba7da7169e8 to your computer and use it in GitHub Desktop.
Save dayfuaim/bea586bacba7da7169e8 to your computer and use it in GitHub Desktop.
Поиск в массиве индекса элемента, ближайшего к заданному. OOPed. :)
package DenisYurashkuFindIndex;
use strict;
use utf8;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {};
bless($self, $class);
$self->{STEPS} = 0;
return $self
}
sub find {
my $self = shift;
my $x = shift; # Number to find index for
my $arr = shift; # Ref to array of numbers where to find the index
my $max = $#$arr;
my $dmin = 1048576;
$self->{STEPS} = 1;
return [0, $self->{STEPS}] if ($x<=$arr->[0]); # Before 0 (or 0) of ARRAY
$self->{STEPS}++;
return [$max, $self->{STEPS}] if ($x>=$arr->[$max]); # Farther than (or exactly at) LEN() of ARRAY
foreach my $i (0..$max) {
my $min_temp = abs($x - $arr->[$i]);
$self->{STEPS}++;
if ($min_temp>=$dmin) {
return [$i-1, $self->{STEPS}]
} else {
$dmin = $min_temp
}
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment