diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:48:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:48:15 +0100 |
| commit | 452785082337f497c33a2f9ce1ed96b35d9a3d4c (patch) | |
| tree | 4acdb48613b742fc4547a28347a7323562395852 | |
| parent | f664a68f4c57f5f99a509376f6c27a429e624221 (diff) | |
| parent | b242ed04e61c70e46edab798faa6f0411eb987c0 (diff) | |
| download | perlweeklychallenge-club-452785082337f497c33a2f9ce1ed96b35d9a3d4c.tar.gz perlweeklychallenge-club-452785082337f497c33a2f9ce1ed96b35d9a3d4c.tar.bz2 perlweeklychallenge-club-452785082337f497c33a2f9ce1ed96b35d9a3d4c.zip | |
Merge pull request #10445 from robbie-hatley/rh278
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #278.
| -rw-r--r-- | challenge-278/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-278/robbie-hatley/perl/ch-1.pl | 94 | ||||
| -rwxr-xr-x | challenge-278/robbie-hatley/perl/ch-2.pl | 86 |
3 files changed, 181 insertions, 0 deletions
diff --git a/challenge-278/robbie-hatley/blog.txt b/challenge-278/robbie-hatley/blog.txt new file mode 100644 index 0000000000..0a7dd93531 --- /dev/null +++ b/challenge-278/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/07/robbie-hatleys-solutions-to-weekly_16.html
\ No newline at end of file diff --git a/challenge-278/robbie-hatley/perl/ch-1.pl b/challenge-278/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..65c5e69c0d --- /dev/null +++ b/challenge-278/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 278-1, +written by Robbie Hatley on Mon Jul 15, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 278-1: Sort String +Submitted by: Mohammad Sajid Anwar +Given a shuffled string, write a script to return the sorted +string. A string is shuffled by appending word position to +each word. + +Example 1 input: +"and2 Raku3 cousins5 Perl1 are4" +Expected output: "Perl and Raku are cousins" + +Example 2 input: +"guest6 Python1 most4 the3 popular5 is2 language7" +Expected output: "Python is the most popular guest language" + +Example 3 input: +"Challenge3 The1 Weekly2" +Expected output: "The Weekly Challenge" + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +My approach was to do this: +1. Split the string by whitespace to array of tokens "@raw_tok". +2. Sort "@raw_tok" to array "@srt_tok" by unicode codepoint of the last character. +3. Join with spaces a map of "@srt_tok" with the last character of each token removed. + +But then I realized, I don't need any of the intermediates, and this is best written in reverse order, +as a purely "functional" subroutine: + use v5.38; + use utf8; + sub sort_string ($string) { + join ' ', + map {substr $_, 0, length($_) - 1} + sort {ord substr($a, -1, 1) <=> ord substr($b, -1, 1)} + split /\s+/, $string + } + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of double-quoted strings of space-separated tokens, with the last character of each token +being a numerical "position" indicator, in proper Perl syntax, like so: +./ch-1.pl '("legs4 shaved2 her3 she1", "dogw hec thel pettedg")' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use utf8; + sub sort_string ($string) { + join ' ', + map {substr $_, 0, length($_) - 1} + sort {ord substr($a, -1, 1) <=> ord substr($b, -1, 1)} + split /\s+/, $string + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @strings = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + "and2 Raku3 cousins5 Perl1 are4", + # Expected output: "Perl and Raku are cousins" + + # Example 2 input: + "guest6 Python1 most4 the3 popular5 is2 language7", + # Expected output: "Python is the most popular guest language" + + # Example 3 input: + "Challenge3 The1 Weekly2", + # Expected output: "The Weekly Challenge" +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $string (@strings) { + say ''; + say 'Shuffled string = ', $string; + say 'Sorted string = ', sort_string $string; +} diff --git a/challenge-278/robbie-hatley/perl/ch-2.pl b/challenge-278/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..cc59c67d9b --- /dev/null +++ b/challenge-278/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,86 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 278-2, +written by Robbie Hatley on Mon Jul 15, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 278-2: Reverse Word +Submitted by: Mohammad Sajid Anwar +Given a word, $word and a character, $char, write a script to +replace the substring up to and including $char with its +characters sorted alphabetically. If $char doesn’t exist in +$word then don't do anything. + +Example 1 input: +["challenge", "e"], +Expected output: "acehllnge" + +Example 2 input: +["programming", "a"], +Expected output: "agoprrmming" + +Example 3 input: +["champion", "b"], +Expected output: "champion" + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll solve this by using a programmatic substitution: + + use v5.38; + use utf8; + sub sort_string ($string, $char) { + $string =~ s{^(.*?$char)}{join '', sort split //, $1}er; + } + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted (array of [word, character] pairs), in proper Perl syntax, like so: +./ch-2.pl '(["enslaved","a"], ["amalgamated","t"])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use utf8; + sub sort_string ($string, $char) { + $string =~ s{^(.*?$char)}{join '', sort split //, $1}er; + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + ["challenge", "e"], + # Expected output: "acehllnge" + + # Example 2 input: + ["programming", "a"], + # Expected output: "agoprrmming" + + # Example 3 input: + ["champion", "b"], + # Expected output: "champion" +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + my $string = $aref->[0]; + my $char = $aref->[1]; + my $sorted = sort_string($string,$char); + say 'Original string = ', $string; + say 'Sorted string = ', $sorted; +} |
