From bb08e3c1a1e3352862f9c795b60a33a9c81ec64a Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 5 Jun 2023 17:52:51 -0400 Subject: Week 220 --- challenge-220/zapwai/perl/ch-1.pl | 13 +++++++++++++ challenge-220/zapwai/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 challenge-220/zapwai/perl/ch-1.pl create mode 100644 challenge-220/zapwai/perl/ch-2.pl diff --git a/challenge-220/zapwai/perl/ch-1.pl b/challenge-220/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..737084de80 --- /dev/null +++ b/challenge-220/zapwai/perl/ch-1.pl @@ -0,0 +1,13 @@ +use v5.30.0; +no warnings; +my @words = ("Perl", "Rust", "Raku"); +say "Input: \@words = (" . join(",",@words) . ")"; +$words[$_] = lc $words[$_] for (0 .. $#words); +my @intersection = split("",$words[0]); +for my $i (1 .. $#words) { + my @letters = split("",$words[$i]); + for my $j (0 .. $#intersection) { + splice(@intersection, $j, 1) unless ($intersection[$j] ~~ @letters); + } +} +say "Output: @intersection"; diff --git a/challenge-220/zapwai/perl/ch-2.pl b/challenge-220/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..eb1e023045 --- /dev/null +++ b/challenge-220/zapwai/perl/ch-2.pl @@ -0,0 +1,33 @@ +use v5.30.0; +use Algorithm::Permute; +my @ints = (1, 17, 8); +#my @ints = (2,2,2); +say "Input: \@ints = (" . join(",",@ints) . ")"; +my $p = Algorithm::Permute->new(\@ints); +print "Output: "; +my $out; +while (my @perm = $p->next) { + my $current_print = "(" . join(",",@perm) . ")"; + next if ($out =~ m/$current_print/); + $out .= $current_print if is_squareful(@perm); +} +$out =~ s/\)\(/\), \(/; +say $out; + +sub is_squareful { + my @list = @_; + my $cnt; + foreach (0 .. $#list-1) { + my $sum = $list[$_] + $list[$_+1]; + $cnt++ if (is_square($sum)); + } + return 1 if ($cnt == $#list); + 0 +} + +sub is_square { + my $num = shift; + my $U = int (sqrt($num)); + return 1 if ($U**2 == $num); + 0 +} -- cgit