Monday, July 22, 2024

TWC279

Challenge Link

Task1

We make a hash out of two arrays and sort it according to values:

#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(zip);

sub sort_letters{
  my %h = map {$_->[0] => $_->[1]} zip $_[0],$_[1];
  join '',sort{$h{$a} <=> $h{$b}} keys %h
}

printf "%s\n",sort_letters(['R','E','P','L'],[3,2,1,4]);
printf "%s\n",sort_letters(['A','U','R','K'],[2,4,1,3]);
printf "%s\n",sort_letters(['O','H','Y','N','P','T'],[5,4,2,6,1,3]);

Task2

tr in perl returns the number of replacements in scalar context and we just check if it is even:
#!/usr/bin/env perl
use strict;
use warnings;

sub split_string{
  $_[0] =~ tr/aeiouy// % 2 == 0
}

printf "%d\n",split_string('perl');
printf "%d\n",split_string('book');
printf "%d\n",split_string('good morning');

Tuesday, July 16, 2024

TWC278

Challenge Link

Task1

We separate the alphas from digits in each word of the string and make a hash from it, then we sort that hash according to the values and return the result as a string:
#!/usr/bin/env perl
use strict;
use warnings;

sub sort_string{
  my %h = map{/(\w+)(\d+)/} split ' ',$_[0];
  join ' ',sort{$h{$a} <=> $h{$b}} keys %h
}

printf "%s\n",sort_string('and2 Raku3 cousins5 Perl1 are4');
printf "%s\n",
  sort_string('guest6 Python1 most4 the3 popular5 is2 language7');
printf "%s\n",sort_string('Challenge3 The1 Weekly2');

Task2

We match the desired part non-greedily, sort it, and substitute it in the given string:
#!/usr/bin/env perl
use strict;
use warnings;

sub reverse_string{
  $_[0] =~ s/(.*?$_[1])/join '',sort split '',$1/er
}

printf "%s\n",reverse_string('challenge','e');
printf "%s\n",reverse_string('programming','a');
printf "%s\n",reverse_string('champion','b');

Wednesday, July 10, 2024

TWC277

Challenge Link

Task1

We count the array elements in a hash and check which ones are equal to two:

#!/usr/bin/env perl
use strict;
use warnings;

sub count_common{
  my %h;
  $h{$_}++ foreach @{$_[0]},@{$_[1]};
  (grep{$_ == 2}values %h) // 0
}

printf "%d\n",
  count_common(['Perl','is','my','friend'],
	       ['Perl','and','Raku','are','friend']);
printf "%d\n",
  count_common(['Perl','and','Python','are','very','similar'],
	       ['Python','is','top','in','guest','languages']);
printf "%d\n",
  count_common(['Perl','is','imperative','Lisp','is','functional'],
	       ['Crystal','is','similar','to','Ruby']);

Task2

We count how many times the given condition is met, for each combination of pairs of elements:

#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(combinations);
use List::AllUtils qw(min uniq);

sub strong_pairs{
  scalar grep{abs($$_[0] - $$_[1]) < min(@$_)}
    combinations([uniq @{$_[0]}],2)
}

printf "%d\n",strong_pairs([1,2,3,4,5]);
printf "%d\n",strong_pairs([5,7,1,7]);

Wednesday, July 3, 2024

TWC276

Challenge Link

Task1

If the sum of any of the pairs is divisible by 24, we increment the count value:

#!usr/bin/env perl
use strict;
use warnings;

sub complete_day{
  my ($arr) = @_;
  my $count = 0;
  foreach my $i(0..$#$arr-1){
    foreach my $j($i+1..$#$arr){
      $count++ if(($arr->[$i] + $arr->[$j]) % 24 == 0)
    }
  }
  $count
}

printf "%d\n",complete_day([12,12,30,24,24]);
printf "%d\n",complete_day([72,48,24,55]);
printf "%d\n",complete_day([12,18,24]);

Task2

We count the values, and sum the ones which have a weight equal to the max weight of the elements:

#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max sum0);

sub maximum_frequency{
  my %h;
  $h{$_}++ foreach @{$_[0]};
  my $max = max values %h;
  sum0 grep {$_ if $_ == $max} values %h
}

printf "%d\n",maximum_frequency([1,2,2,4,1,5]);
printf "%d\n",maximum_frequency([1,2,3,4,5]);