aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2023-06-05 09:36:00 +0000
committerNiels van Dijke <perlboy@cpan.org>2023-06-05 09:36:00 +0000
commit2791165166fc1dcd480c190db7f01f79ac03f73e (patch)
tree384ef5f61626374e041e25322f87075a6a488d0e
parent401be1861472af6d62bbdeb0fe65f6ced1ca8f31 (diff)
downloadperlweeklychallenge-club-2791165166fc1dcd480c190db7f01f79ac03f73e.tar.gz
perlweeklychallenge-club-2791165166fc1dcd480c190db7f01f79ac03f73e.tar.bz2
perlweeklychallenge-club-2791165166fc1dcd480c190db7f01f79ac03f73e.zip
w220 - Task 1 & 2
-rwxr-xr-xchallenge-220/perlboy1967/perl/ch1.pl39
-rwxr-xr-xchallenge-220/perlboy1967/perl/ch2.pl54
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-220/perlboy1967/perl/ch1.pl b/challenge-220/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..2166bd4e3f
--- /dev/null
+++ b/challenge-220/perlboy1967/perl/ch1.pl
@@ -0,0 +1,39 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 220
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-220
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Common Characters
+Submitted by: Mohammad S Anwar
+
+You are given a list of words.
+
+Write a script to return the list of common characters (sorted alphabeticall)
+found in every word of the given list.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+use List::MoreUtils qw(uniq);
+
+sub commonChars (@) {
+ my %c;
+ map { $c{lc $_}++ } uniq split // for (@_);
+ sort grep { $c{$_} >= scalar @_ } keys %c;
+}
+
+cmp_deeply([commonChars(qw(Perl Rust Raku))],[qw(r)]);
+cmp_deeply([commonChars(qw(love live leave))],[qw(e l v)]);
+cmp_deeply([commonChars(qw(bamboo bank))],[qw(a b)]);
+
+done_testing;
diff --git a/challenge-220/perlboy1967/perl/ch2.pl b/challenge-220/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..8dea4cc4a6
--- /dev/null
+++ b/challenge-220/perlboy1967/perl/ch2.pl
@@ -0,0 +1,54 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 220
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-220
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Squareful
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers, @ints.
+
+|| An array is squareful if the sum of every pair of adjacent elements is a perfect square.
+
+Write a script to find all the permutations of the given array that are squareful.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+use List::MoreUtils qw(uniq slide all);
+use Algorithm::Combinatorics qw(permutations);
+use Data::Compare;
+
+
+sub _areSquareful ($$) {
+ my $s = $_[0] + $_[1];
+ state $c = {};
+ $c->{$s} //= int($s**0.5) == $s**0.5;
+ return $c->{$s};
+}
+
+sub squareful (\@) {
+ my ($ar,$u,@r) = ($_[0],uniq(@{$_[0]}) == 1);
+
+ my $iter = permutations($ar);
+ while (my $arC = $iter->next) {
+ push(@r,$arC) if all {$_} slide {_areSquareful($a,$b)} @$arC;
+ last if (scalar @r && $u);
+ }
+ sort { Compare($a,$b) } @r;
+}
+
+cmp_deeply([squareful(@{[1,17,8]})],[[1,8,17],[17,8,1]]);
+cmp_deeply([squareful(@{[2,2,2]})],[[2,2,2]]);
+
+done_testing;