aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-09-11 09:25:07 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-09-11 09:25:07 -0600
commitc2f37f15a35dc0146cc7cbd3f13f2f5fabed02a3 (patch)
tree2b7a9242ed486348ecea76a1b50c465fe35da79a
parent3c9efcab101672b5ecfa042ca258f93d81474928 (diff)
downloadperlweeklychallenge-club-c2f37f15a35dc0146cc7cbd3f13f2f5fabed02a3.tar.gz
perlweeklychallenge-club-c2f37f15a35dc0146cc7cbd3f13f2f5fabed02a3.tar.bz2
perlweeklychallenge-club-c2f37f15a35dc0146cc7cbd3f13f2f5fabed02a3.zip
solve pwc234
-rw-r--r--challenge-234/wlmb/blog.txt1
-rwxr-xr-xchallenge-234/wlmb/perl/ch-1.pl27
-rwxr-xr-xchallenge-234/wlmb/perl/ch-2.pl21
3 files changed, 49 insertions, 0 deletions
diff --git a/challenge-234/wlmb/blog.txt b/challenge-234/wlmb/blog.txt
new file mode 100644
index 0000000000..38fcd698e6
--- /dev/null
+++ b/challenge-234/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2023/09/11/PWC234/
diff --git a/challenge-234/wlmb/perl/ch-1.pl b/challenge-234/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..1b38c6413b
--- /dev/null
+++ b/challenge-234/wlmb/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 234
+# Task 1: Common Characters
+#
+# See https://wlmb.github.io/2023/09/11/PWC234/#task-1-common-characters
+use v5.36;
+use List::Util qw(min);
+die <<~"FIN" unless @ARGV;
+ Usage: $0 W1 [W2...]
+ to find the common characters in all words W1, W2...
+ FIN
+my @letters_of_word;
+for(@ARGV){
+ my %count_of_letter;
+ $count_of_letter{$_}++ for split "";
+ push @letters_of_word, \%count_of_letter;
+}
+my $number_of_words=@letters_of_word;
+my @result=map {
+ my $letter=$_;
+ my $repetition=min map {
+ my $word_number=$_;
+ $letters_of_word[$word_number]{$letter}//0
+ } 0..$number_of_words-1;
+ ($letter) x $repetition;
+} keys %{$letters_of_word[0]};
+say "@ARGV -> @result";
diff --git a/challenge-234/wlmb/perl/ch-2.pl b/challenge-234/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..1926cc7bd6
--- /dev/null
+++ b/challenge-234/wlmb/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 234
+# Task 2: Unequal Triplets
+#
+# See https://wlmb.github.io/2023/09/11/PWC234/#task-2-unequal-triplets
+use v5.36;
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(sum0 product);
+die <<~"FIN" unless @ARGV;
+ Usage: $0 N1 [N2...]
+ to count the number of unequal triplets in the set {N1 N2...}
+ FIN
+my %count_for_number;
+++$count_for_number{$_} for @ARGV;
+my @numbers=keys %count_for_number;
+my $result=@numbers<3
+ ? 0
+ : sum0 map {
+ product @count_for_number{@$_}
+} combinations(\@numbers,3);
+say "@ARGV -> $result",