From 28bf4e1de4fa641b1e8e3b11db48a84fe40b218e Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 15 Jul 2024 16:04:03 +0100 Subject: Week 278 - Tangled string and drow --- challenge-278/peter-campbell-smith/blog.txt | 1 + challenge-278/peter-campbell-smith/perl/ch-1.pl | 28 ++++++++++++++++++++++++ challenge-278/peter-campbell-smith/perl/ch-2.pl | 29 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 challenge-278/peter-campbell-smith/blog.txt create mode 100755 challenge-278/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-278/peter-campbell-smith/perl/ch-2.pl 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); +} -- cgit