diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-06-10 18:45:27 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-06-10 18:45:27 +0800 |
| commit | 8b05b2fbe3eaf90c92aa624abf6719cd731d7ada (patch) | |
| tree | 00af25810446d7e479b99dea71c0d727db5d1daf | |
| parent | 266becae2fc5391bc78995e9e2ade1507b8e1cee (diff) | |
| download | perlweeklychallenge-club-8b05b2fbe3eaf90c92aa624abf6719cd731d7ada.tar.gz perlweeklychallenge-club-8b05b2fbe3eaf90c92aa624abf6719cd731d7ada.tar.bz2 perlweeklychallenge-club-8b05b2fbe3eaf90c92aa624abf6719cd731d7ada.zip | |
Week 220
| -rw-r--r-- | challenge-220/cheok-yin-fung/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-220/cheok-yin-fung/perl/ch-2.pl | 50 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-220/cheok-yin-fung/perl/ch-1.pl b/challenge-220/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..0bb2d0d9c6 --- /dev/null +++ b/challenge-220/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,26 @@ +# The Weekly Challenge 220 +# Task 1 Common Characters +use v5.30.0; +use warnings; +use Set::Scalar; + +sub cc { + my @words = @_; + @words = map {lc} @words; + my $first_w = shift @words; + my $ccs = Set::Scalar->new(split "", $first_w); + for my $w (@words) { + my $cs = Set::Scalar->new(split "", $w); + $ccs = $ccs->intersection($cs); + } + return [sort {$a cmp $b} $ccs->elements]; + +} + +use Test::More tests=>3; +use Test::Deep; +cmp_deeply cc("Perl", "Rust", "Raku"), ["r"]; +cmp_deeply cc("love", "live", "leave"), ["e", "l", "v"]; +use utf8; +ok (Set::Scalar->new(cc("一石二鳥", "一二三四")->@*)->is_equal(Set::Scalar->new("一", "二"))); + diff --git a/challenge-220/cheok-yin-fung/perl/ch-2.pl b/challenge-220/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..334bee4707 --- /dev/null +++ b/challenge-220/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,50 @@ +# The Weekly Challenge 220 +# Task 2 Squareful +# Brute-force approach +use v5.30.0; +use warnings; +use Math::Permutation; +use List::Util qw/all/; +use List::MoreUtils qw/pairwise/; + + +sub _fact { + return 1 if $_[0] == 0; + return $_[0] * _fact($_[0]-1); +} + +sub is_perfect_sq { + my $a = sqrt($_); + return $a !~ /\./; +} + +sub check_squareful { + my @num = @_; + my @num0 = @num[0..$#num-1]; + my @num1 = @num[1..$#num]; + my @x = pairwise {$a+$b} @num0, @num1; + return all {is_perfect_sq} @x; +} + +sub sqf { + my @num = @_; + my %ans; + my $p = Math::Permutation->init(scalar @num); + for (1.._fact(scalar @num)) { + my @permuted_num = map {$num[$_-1]} $p->array; + $ans{join ",", @permuted_num} = 1 if check_squareful(@permuted_num); + $p->nxt; + } + return keys %ans; +} + +say join "\n", sqf(1, 17, 8); +say ""; +say join "\n", sqf(2, 2, 2); + +say join "\n", sqf(10,6,3,13,12); + +# Follow-up +# Is there a distinct sequence of positive integers such that it has more than 3 squareful arrangements? +# See also: +# https://math.stackexchange.com/questions/1168983/arranging-numbers-from-1-to-n-such-that-the-sum-of-every-two-adjacent-number |
