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]);

No comments:

Post a Comment