aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-15 04:57:22 +0100
committerGitHub <noreply@github.com>2023-05-15 04:57:22 +0100
commit7069e7a899680d60f05667eaca095773b10bafd7 (patch)
tree4e669f322be6e5f83236a866566b104d16ba4d81
parent19412ea326814b3b74a0220230881e76df7ca63d (diff)
parent271a7951453e9f875baf518cb406cda088add12c (diff)
downloadperlweeklychallenge-club-7069e7a899680d60f05667eaca095773b10bafd7.tar.gz
perlweeklychallenge-club-7069e7a899680d60f05667eaca095773b10bafd7.tar.bz2
perlweeklychallenge-club-7069e7a899680d60f05667eaca095773b10bafd7.zip
Merge pull request #8076 from jaldhar/challenge-216
Challenge 216 by Jaldhar H. Vyas.
-rw-r--r--challenge-216/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-216/jaldhar-h-vyas/perl/ch-1.pl49
-rwxr-xr-xchallenge-216/jaldhar-h-vyas/perl/ch-2.pl89
-rwxr-xr-xchallenge-216/jaldhar-h-vyas/raku/ch-1.raku17
-rwxr-xr-xchallenge-216/jaldhar-h-vyas/raku/ch-2.raku26
5 files changed, 182 insertions, 0 deletions
diff --git a/challenge-216/jaldhar-h-vyas/blog.txt b/challenge-216/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..5cfbd875fe
--- /dev/null
+++ b/challenge-216/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2023/05/perl_weekly_challenge_week_216.html
diff --git a/challenge-216/jaldhar-h-vyas/perl/ch-1.pl b/challenge-216/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..c7769971e3
--- /dev/null
+++ b/challenge-216/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub isSubset {
+ my @subset = @{$_[0]};
+
+ my %set;
+ for my $c (@{$_[1]}) {
+ $set{$c}++;
+ }
+
+ for my $c (@subset) {
+ if (!exists $set{$c} || $set{$c} == 0) {
+ return undef;
+ }
+ $set{$c}--;
+ }
+
+ return 1;
+}
+
+
+sub combGrepUniqueLc {
+ my ($str) = @_;
+
+ my @chars = map { lc } grep { $_ =~ /[[:alpha:]]/ } split //, $str;
+
+ my %unique;
+ for my $c (@chars) {
+ $unique{$c}++;
+ }
+
+ return keys %unique;
+}
+
+my ($reg, @words) = @ARGV;
+
+my @registration = combGrepUniqueLc($reg);
+my @results;
+
+for my $word (@words) {
+ my @w = combGrepUniqueLc($word);
+ if (isSubset(\@registration, \@w)) {
+ push @results, $word;
+ }
+}
+
+say q{(}, (join q{, }, map {"'$_'"} @results), q{)};
diff --git a/challenge-216/jaldhar-h-vyas/perl/ch-2.pl b/challenge-216/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..368e974b8c
--- /dev/null
+++ b/challenge-216/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,89 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub combUnique {
+ my ($str) = @_;
+
+ my @chars = split //, $str;
+
+ my %unique;
+ for my $c (@chars) {
+ $unique{$c}++;
+ }
+
+ return keys %unique;
+}
+
+sub isSubset {
+ my @subset = @{$_[0]};
+
+ my %set;
+ for my $c (@{$_[1]}) {
+ $set{$c}++;
+ }
+
+ for my $c (@subset) {
+ if (!exists $set{$c} || $set{$c} == 0) {
+ return undef;
+ }
+ $set{$c}--;
+ }
+
+ return 1;
+}
+
+sub xx {
+ my ($array, $amount) = @_;
+ my @result = @{$array};
+
+ for (1 .. $amount - 1) {
+ push @result, @{$array};
+ }
+
+ return @result;
+}
+
+my ($word, @stickers) = @ARGV;
+
+unless (isSubset([combUnique($word)], [combUnique(join q{}, @stickers)])) {
+ say 0;
+ exit;
+}
+
+my @stickerList = xx(\@stickers, scalar combUnique($word));
+my @chars = split //, $word;
+my $result = 0;
+
+LOOP: for my $i (1 .. scalar @stickerList) {
+ for my $combo (combinations(\@stickerList, $i)) {
+ my @sticker = split //, (join q{}, @{$combo});
+ if (isSubset(\@chars, \@sticker)) {
+ $result = scalar @{$combo};
+ last LOOP;
+ }
+ }
+}
+
+say $result;
diff --git a/challenge-216/jaldhar-h-vyas/raku/ch-1.raku b/challenge-216/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..610fcff0ad
--- /dev/null
+++ b/challenge-216/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ $reg, *@words
+) {
+ my @registration = $reg.comb.grep({$_ ~~ /<alpha>/}).unique.map({ $_.lc });
+ my @results;
+
+ for @words -> $word {
+ my @w = $word.comb.grep({$_ ~~ /<alpha>/}).unique.map({ $_.lc });
+ if @registration ⊆ @w {
+ @results.push($word);
+ }
+ }
+
+ say q{(}, @results.map({"'$_'"}).join(q{, }), q{)};
+} \ No newline at end of file
diff --git a/challenge-216/jaldhar-h-vyas/raku/ch-2.raku b/challenge-216/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..6d314a1bca
--- /dev/null
+++ b/challenge-216/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ $word, *@stickers
+) {
+
+ if $word.comb.unique ⊈ @stickers.join.comb.unique {
+ say 0;
+ exit;
+ }
+ my @stickerList = (@stickers xx $word.comb.unique.elems).flat;
+ my $chars = Bag.new($word.comb);
+ my $result = 0;
+
+ LOOP: for 1 .. @stickerList.elems -> $i {
+ for @stickerList.combinations($i) -> @combo {
+ my $sticker = Bag.new(@combo.join.comb);
+ if $chars ⊆ $sticker {
+ $result = @combo.elems;
+ last LOOP;
+ }
+ }
+ }
+
+ say $result;
+} \ No newline at end of file