aboutsummaryrefslogtreecommitdiff
path: root/challenge-278
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-07-15 16:04:03 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-07-15 16:04:03 +0100
commit28bf4e1de4fa641b1e8e3b11db48a84fe40b218e (patch)
tree142a6a1afa0851b233907d0ca8898856a8695c8b /challenge-278
parentf1533357698083086127e85e17fd8e2a80780e76 (diff)
downloadperlweeklychallenge-club-28bf4e1de4fa641b1e8e3b11db48a84fe40b218e.tar.gz
perlweeklychallenge-club-28bf4e1de4fa641b1e8e3b11db48a84fe40b218e.tar.bz2
perlweeklychallenge-club-28bf4e1de4fa641b1e8e3b11db48a84fe40b218e.zip
Week 278 - Tangled string and drow
Diffstat (limited to 'challenge-278')
-rw-r--r--challenge-278/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-278/peter-campbell-smith/perl/ch-1.pl28
-rwxr-xr-xchallenge-278/peter-campbell-smith/perl/ch-2.pl29
3 files changed, 58 insertions, 0 deletions
diff --git a/challenge-278/peter-campbell-smith/blog.txt b/challenge-278/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..43450c7878
--- /dev/null
+++ b/challenge-278/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/278
diff --git a/challenge-278/peter-campbell-smith/perl/ch-1.pl b/challenge-278/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..0ff6f9d831
--- /dev/null
+++ b/challenge-278/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-07-15
+use utf8; # Week 278 - task 1 - Sort string
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+# You are given a shuffle string, $str.
+# Write a script to return the sorted string.
+# A string is shuffled by appending word position to each word.
+
+sort_string('and2 Raku3 cousins5 Perl1 are4');
+sort_string('A1 appending6 by5 each10 is3 position8 shuffled4 string2 to9 word7 word11');
+sort_string('a3 challenge11 is2 line5 one4 solution6 Sort9 String10 the8 This1 to7');
+
+sub sort_string {
+
+ my ($string, @new);
+ $string = shift;
+
+ # new[$n - 1] is the $n'th word in the output string
+ @new[$2 - 1] = $1 while $string =~ m|([a-z]+)(\d+)|gi;
+
+ printf(qq[\nInput: \@string = '%s'\n], $string);
+ printf(qq[Output: '%s'\n], join(' ', @new));
+}
diff --git a/challenge-278/peter-campbell-smith/perl/ch-2.pl b/challenge-278/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..801db1604d
--- /dev/null
+++ b/challenge-278/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-07-15
+use utf8; # Week 278 - task 2 - Reverse word
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+reverse_word('challenge', 'e');
+reverse_word('programming', 'a');
+reverse_word('champion', 'b');
+reverse_word('testing', 't');
+reverse_word('multiplication', 'n');
+
+sub reverse_word {
+
+ my ($word, $char, $output);
+
+ ($word, $char) = @_;
+ if ($word =~ m|(.*?$char)(.*)|) {
+ $output = join('', sort(split(//, $1))) . $2;
+ } else {
+ $output = $word;
+ }
+
+ printf(qq[\nInput: \$word = '%s', \$char = '%s'\n], $word, $char);
+ printf(qq[Output: '%s'\n], $output);
+}