diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:46:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:46:51 +0100 |
| commit | ea65838314bb5eadb8cfbd1fd9677d4d1bd20d12 (patch) | |
| tree | 99abeb37169a9480f60e665eb07d0df542682d2a /challenge-278 | |
| parent | 9e0591a9b4c82d506bc39a1091d499c15c47c21b (diff) | |
| parent | a4f42ce60e00e7d5fe3584cd6f7aa9a6d0983c17 (diff) | |
| download | perlweeklychallenge-club-ea65838314bb5eadb8cfbd1fd9677d4d1bd20d12.tar.gz perlweeklychallenge-club-ea65838314bb5eadb8cfbd1fd9677d4d1bd20d12.tar.bz2 perlweeklychallenge-club-ea65838314bb5eadb8cfbd1fd9677d4d1bd20d12.zip | |
Merge pull request #10442 from wlmb/challenges
Solve PWC278
Diffstat (limited to 'challenge-278')
| -rw-r--r-- | challenge-278/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-278/wlmb/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-278/wlmb/perl/ch-2.pl | 18 |
3 files changed, 41 insertions, 0 deletions
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" +} |
