From a8609bdf63cf6d5f0780c676b5cf4d48ecd72cbb Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 21 Jul 2025 08:15:29 -0600 Subject: Solve PWC331 --- challenge-331/wlmb/blog.txt | 1 + challenge-331/wlmb/perl/ch-1.pl | 17 +++++++++++++++++ challenge-331/wlmb/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 challenge-331/wlmb/blog.txt create mode 100755 challenge-331/wlmb/perl/ch-1.pl create mode 100755 challenge-331/wlmb/perl/ch-2.pl diff --git a/challenge-331/wlmb/blog.txt b/challenge-331/wlmb/blog.txt new file mode 100644 index 0000000000..837eeaee1c --- /dev/null +++ b/challenge-331/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/07/21/PWC331/ diff --git a/challenge-331/wlmb/perl/ch-1.pl b/challenge-331/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..f49164cec9 --- /dev/null +++ b/challenge-331/wlmb/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +# Perl weekly challenge 331 +# Task 1: Last Word +# +# See https://wlmb.github.io/2025/07/21/PWC331/#task-1-last-word +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 S1 S2... + to find the length of the last word of the strings Sn + FIN +for(@ARGV){ + say "$_ -> ", # print the result + length # compute the length + [split " "] # separate into words + ->[-1] # choose last word + =~s/\W//gr # remove non-word characters +} diff --git a/challenge-331/wlmb/perl/ch-2.pl b/challenge-331/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..51f3e6f145 --- /dev/null +++ b/challenge-331/wlmb/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 331 +# Task 2: Buddy Strings +# +# See https://wlmb.github.io/2025/07/21/PWC331/#task-2-buddy-strings +use v5.36; +die <<~"FIN" unless @ARGV && @ARGV%2==0; + Usage: $0 S1 T1 S2 T2... + to find if target string Tn may be obtained from the source string Sn + by a single permutation of two characters. + FIN +for my($source, $target)(@ARGV){ + say"$source $target -> ", + (my $joined="$source$;$target") # join source and target using special character + =~ # match to + /^ # start of source + (.*) # arbitrary string (group 1) + (.) # a single character to permute (group 2) + (.*) # arbitrary string (group 3) + (.) # second character to permute (group 4) + (.*) # arbitrary string (group 5) + $; # end of source + \1\4\3\2\5 # target is source with \2 and \4 permuted + $ # end of target + /x + ?"True":"False"; +} -- cgit