Tuesday, April 29, 2025

TWC319

Challenge Link

Task1

We check if the first or last character the strings are vowels and count them:
#!/usr/bin/env perl
use strict;
use warnings;

sub word_count{
  scalar grep {/^[aeiou]|[aeiou]$/} @{$_[0]}
}

printf "%d\n",word_count(["unicode","xml","raku","perl"]);
printf "%d\n",word_count(["the","weekly","challenge"]);
printf "%d\n",word_count(["perl","python","postgres"]);

Task2

We find the common items in the two arrays, then find the minimum:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(min);
use Set::Scalar;

sub minimum_common{
  (min (Set::Scalar->new(@{$_[0]})
	->intersection(Set::Scalar->new(@{$_[1]}))
	->members)) // -1
}

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

Sunday, April 27, 2025

TWC318

Challenge Link

Task1

We return an array of runs of 3 or more characters:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub group_position{
  my @ret;
  push @ret,$1.$2 while($_[0] =~ m/(.)(\1{2,})/g);
  @ret
}

print show group_position('abccccd');
print show group_position('aaabcddddeefff');
print show group_position('abcdd');

Task2

We check if the two arrays are reverse of each other:

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

sub reverse_equals{
  my ($src,$target) = @_;
  my $from = first {$src->[$_] != $target->[$_]} 0..$#$src;
  return 1 unless defined $from;
  my $to = first {$src->[$_] != $target->[$_]} reverse $from..$#$src;
  foreach my $i(0..$to-$from){
    return 0 if $src->[$from+$i] != $target->[$to-$i]
  }
  1;
}

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

Monday, April 14, 2025

TWC317

Challenge Link

Task1

We join first character of each string and check if it is equal to the given word:
#!/usr/bin/env perl
use strict;
use warnings;

sub acronyms {
  (join '',map{substr $_,0,1} @{$_[0]}) eq $_[1];
}

printf"%d\n",acronyms(['Perl','Weekly','Challenge'],'PWC');
printf"%d\n",acronyms(['Bob','Charlie','Joe'],'BCJ');
printf"%d\n",acronyms(['Morning','Good'],'MM');

Task2

We can make two sets from the strings or sort the strings and check if they are equal, both methods work:
#!/usr/bin/env perl
use strict;
use warnings;

sub friendly_strings {
  (join '',sort split '',$_[0]) eq (join '',sort split '',$_[1])
}

printf "%d\n",friendly_strings('desc','dsec');
printf "%d\n",friendly_strings('fuck','fcuk');
printf "%d\n",friendly_strings('poo','eop');
printf "%d\n",friendly_strings('stripe','sprite');

Monday, April 7, 2025

TWC316

Challenge Link

Task1

For each pair we check if the last character of first one is equal to first of second:

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

sub circular{
  for(my $i = 0; $i < $#{$_[0]}; $i++) {
    my $l = substr($_[0]->[$i],-1);
    my $f = substr($_[0]->[$i+1],0,1);
    return 0 unless $l eq $f
  }
  1
}

printf "%d\n",circular(['perl','loves','scala']);
printf "%d\n",circular(['love','the','programming']);
printf "%d\n",circular(['java','awk','kotlin','node.js']);

Task2

We check if the characters of first string occur in the second in order:

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

sub subsequence{
  my @idx = map{index $_[1],$_} split '',$_[0];
  join('',@idx) eq join '',sort{$a <=> $b} @idx
}

printf "%d\n",subsequence('uvw','bcudvew');
printf "%d\n",subsequence('aec','abcde');
printf "%d\n",subsequence('sip','javascript');