Tuesday, April 9, 2024

TWC264

Challenge Link

Task1

We make two sets of lowercase and uppercase letters and find their intersection:
#!/usr/local/bin/env perl
use strict;
use warnings;
use Set::Scalar;
use List::Util qw(maxstr);

sub greatest_english_letter{
  my $s1 = Set::Scalar->new;
  my $s2 = Set::Scalar->new;
  map{$_ le 'Z' ? $s1->insert($_) : $s2->insert(uc $_)} split '',$_[0];
  maxstr($s1->intersection($s2)->members) // ''
}

printf "%s\n", greatest_english_letter('PeRlwEeKLy');
printf "%s\n", greatest_english_letter('ChaLlenge');
printf "%s\n", greatest_english_letter('The');

Task2

We insert the elements at appropriate indices as per the given instructions:

#!/usr/local/bin/env perl
use strict;
use warnings;
use Data::Show;

sub target_array{
  my @ret;
  splice @ret,$_[1]->[$_],0,$_[0]->[$_] foreach(0..$#{$_[1]});
  @ret
}

print show target_array([0,1,2,3,4],[0,1,2,2,1]);
print show target_array([1,2,3,4,0],[0,1,2,3,0]);
print show target_array([1],[0]);

No comments:

Post a Comment