The Weekly Challenge - 263


TASK #1: Target Index
You are given an array of integers, @ints and a target element $k.

Write a script to return the list of indices in the sorted array where the element is same as the given target element.


Here my solution, without keyword 'my', without validating arguments subroutine, without a module:


sub mc_target_index {
 ($k_, @ints_) = @_;  	

  @ints_sorted = ();
  @ints_sorted = sort( { $a <=> $b } @ints_ );
  join(", ", grep { $ints_sorted[$_] == $k_ } 0 .. $#ints_sorted);
}

@ints = (1, 5, 3, 2, 4, 2);
$k = 2;
print("\(", mc_target_index($k, @ints), ")\n"); # Output: (1,2)

@ints = (1, 2, 4, 3, 5);
$k = 6;
print("\(", mc_target_index($k, @ints), ")\n"); # Output: ()

@ints = (5, 3, 2, 4, 2, 1);
$k = 4;
print("\(", mc_target_index($k, @ints), ")\n"); # Output: (4)