aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-19 22:08:42 +0100
committerGitHub <noreply@github.com>2024-07-19 22:08:42 +0100
commit53eb3e8513731c939cc0dd994148e54518c20f38 (patch)
tree750692f506fb9e9fbdc38a3faaa47a680492d92e
parentb9108492b4fb913e4c54f7847aea1c2db650c115 (diff)
parent7f1b1455b1d779fd28ebc0eb0e117c047ba9e62d (diff)
downloadperlweeklychallenge-club-53eb3e8513731c939cc0dd994148e54518c20f38.tar.gz
perlweeklychallenge-club-53eb3e8513731c939cc0dd994148e54518c20f38.tar.bz2
perlweeklychallenge-club-53eb3e8513731c939cc0dd994148e54518c20f38.zip
Merge pull request #10456 from jo-37/contrib
Solutions to challenge 278
-rw-r--r--challenge-278/jo-37/blog.txt1
-rwxr-xr-xchallenge-278/jo-37/perl/ch-1.pl71
-rwxr-xr-xchallenge-278/jo-37/perl/ch-2.pl67
3 files changed, 139 insertions, 0 deletions
diff --git a/challenge-278/jo-37/blog.txt b/challenge-278/jo-37/blog.txt
new file mode 100644
index 0000000000..3fe30a70ee
--- /dev/null
+++ b/challenge-278/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/07/19/ch-278.html
diff --git a/challenge-278/jo-37/perl/ch-1.pl b/challenge-278/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..7e1f466199
--- /dev/null
+++ b/challenge-278/jo-37/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [SHUFFLED_WORDS]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+SHUFFLED_WORDS
+ string or list of shuffled words
+
+EOS
+
+
+### Input and Output
+
+say sort_string("@ARGV");
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/07/19/ch-278.html#task-1
+
+
+sub sort_string {
+ join " ",
+ map $_->[0],
+ sort {$a->[1] <=> $b->[1]}
+ map [/(.+?)(\d+)$/],
+ split /\s+/, shift;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is sort_string("and2 Raku3 cousins5 Perl1 are4"),
+ "Perl and Raku are cousins", "example 1";
+
+ is sort_string("guest6 Python1 most4 the3 popular5 is2 language7"),
+ "Python is the most popular guest language", "example 2";
+
+ is sort_string("Challenge3 The1 Weekly2"),
+ "The Weekly Challenge", "example 3";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is sort_string("twelve12 one1 two2 eight8 six6 seven7 five5 ten10 four4 eleven11 nine9 three3"),
+ "one two three four five six seven eight nine ten eleven twelve",
+ "more than ten";
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-278/jo-37/perl/ch-2.pl b/challenge-278/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..5a25e71b63
--- /dev/null
+++ b/challenge-278/jo-37/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [STR CHR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+STR
+ a string
+
+CHR
+ a character (or substring)
+
+EOS
+
+
+### Input and Output
+
+say reverse_word(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/07/19/ch-278.html#task-2
+
+
+sub reverse_word ($str, $char) {
+
+ $str =~ s#(.*?\Q$char\E)#join '', sort split //, $1#er;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is reverse_word("challenge", "e"), "acehllnge", "example 1";
+ is reverse_word("programming", "a"), "agoprrmming", "example 2";
+ is reverse_word("champion", "b"), "champion", "example 3";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is reverse_word("keep", "k"), "keep", "match first";
+ }
+
+ done_testing;
+ exit;
+}