The Weekly Challenge - 264


TASK #1: Greatest English Letter
You are given a string, $str, made up of only alphabetic characters [a..zA..Z].

Write a script to return the greatest english letter in the given string.

A letter is greatest if it occurs as lower and upper case. Also letter ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.


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


sub greatest_english_letter {

   $str_ = shift;	
   @matches = ();

   foreach $char ($str_ =~ /[A-Z]/g) {
     push(@matches, $char) if( index($str_, lc($char)) != -1 ); 
   }
  
   @matches = sort(@matches);
   ($matches[-1]) ? ( print($matches[-1] . "\n") ) : print("No match\n");
}

$str = 'PeRlwEeKLy';
greatest_english_letter($str);# Output: L

$str = 'ChaLlenge';
greatest_english_letter($str);# Output: L

$str = 'The';
greatest_english_letter($str);# Output: No match

TASK #2: Target Array
You are given two arrays of integers, @source and @indices. The @indices can only contains integers 0 <= i < size of @source. Write a script to create target array by insert at index $indices[i] the value $source[i].

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

sub target_array {
	
  ($aref_1, $aref_2) = @_;
  @source_ = @$aref_1;
  @indices_ = @$aref_2;
	
  $target_string = "";	
  $index = 0;

  foreach $i (@source_) {
      substr($target_string, $indices_[$index], 0) = $i;
      $index++;
  }

  @target_array = split(//, $target_string);
  print("@target_array\n");
}

@source  = (0, 1, 2, 3, 4);
@indices = (0, 1, 2, 2, 1);
target_array (\@source, \@indices); # Output: (0, 4, 1, 3, 2)

@source  = (1, 2, 3, 4, 0);
@indices = (0, 1, 2, 3, 0);
target_array (\@source, \@indices); # Output: (0, 1, 2, 3, 4)

@source  = (1);
@indices = (0);
target_array (\@source, \@indices); # Output: (1)