From a4f42ce60e00e7d5fe3584cd6f7aa9a6d0983c17 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 15 Jul 2024 12:38:02 -0600 Subject: Solve PWC278 --- challenge-278/wlmb/blog.txt | 1 + challenge-278/wlmb/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-278/wlmb/perl/ch-2.pl | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 challenge-278/wlmb/blog.txt create mode 100755 challenge-278/wlmb/perl/ch-1.pl create mode 100755 challenge-278/wlmb/perl/ch-2.pl diff --git a/challenge-278/wlmb/blog.txt b/challenge-278/wlmb/blog.txt new file mode 100644 index 0000000000..d598a3ceae --- /dev/null +++ b/challenge-278/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/07/15/PWC278/ diff --git a/challenge-278/wlmb/perl/ch-1.pl b/challenge-278/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..944fa3924e --- /dev/null +++ b/challenge-278/wlmb/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 278 +# Task 1: Sort String +# +# See https://wlmb.github.io/2024/07/15/PWC278/#task-1-sort-string +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 S1 S2... + Sort the strings S1 S2... + Each string is formed of space-separated word-number strings, i.e., a word concatenated to a + non-negative number giving its position. + FIN +for(@ARGV){ + my @pair_strings=split " ", $_; + my @pairs; + for(@pair_strings){ + /^(.+)?([0-9]+)$/ or die "Expected WD where W are word characters and D are digits: $_"; + push @pairs, [$1, $2]; + } + my @sorted=map {$_->[0]} sort {$a->[1] <=> $b->[1] || lc $a->[0] cmp lc $b->[0]} @pairs; + say "$_ -> @sorted"; +} diff --git a/challenge-278/wlmb/perl/ch-2.pl b/challenge-278/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..d9b63ac872 --- /dev/null +++ b/challenge-278/wlmb/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 278 +# Task 2: Reverse Word +# +# See https://wlmb.github.io/2024/07/15/PWC278/#task-2-reverse-word +use v5.36; +use experimental qw(for_list); +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 S1 C1 S2 C2... + to sort the characters of each string Si upto the character Ci + FIN +for my($string, $char)(@ARGV){ + warn "Expected a single character: $char" unless length $char == 1; + my $out=$string =~ /^(.*?$char)(.*)$/ + ? join("", sort{$a cmp $b} split "", $1) . $2 + : $string; + say "$string $char -> $out" +} -- cgit