diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-07-15 19:59:34 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-07-15 19:59:34 +0200 |
| commit | 02ee03002d672694516250a25bd5c8310d0c2023 (patch) | |
| tree | 575e0bbf30a8b85697c053c604ed36e03dfc47c2 | |
| parent | f1533357698083086127e85e17fd8e2a80780e76 (diff) | |
| download | perlweeklychallenge-club-02ee03002d672694516250a25bd5c8310d0c2023.tar.gz perlweeklychallenge-club-02ee03002d672694516250a25bd5c8310d0c2023.tar.bz2 perlweeklychallenge-club-02ee03002d672694516250a25bd5c8310d0c2023.zip | |
Add solution 278.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-278/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-278/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-278/jeanluc2020/perl/ch-1.pl | 60 | ||||
| -rwxr-xr-x | challenge-278/jeanluc2020/perl/ch-2.pl | 54 |
4 files changed, 116 insertions, 0 deletions
diff --git a/challenge-278/jeanluc2020/blog-1.txt b/challenge-278/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..19a83dff83 --- /dev/null +++ b/challenge-278/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-278-1.html diff --git a/challenge-278/jeanluc2020/blog-2.txt b/challenge-278/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..49dae3f9b2 --- /dev/null +++ b/challenge-278/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-278-2.html diff --git a/challenge-278/jeanluc2020/perl/ch-1.pl b/challenge-278/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..e03805ac5b --- /dev/null +++ b/challenge-278/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-278/#TASK1 +# +# Task 1: Sort String +# =================== +# +# 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. +# +## Example 1 +## +## Input: $str = "and2 Raku3 cousins5 Perl1 are4" +## Output: "Perl and Raku are cousins" +# +## Example 2 +## +## Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7" +## Output: "Python is the most popular guest language" +# +## Example 3 +## +## Input: $str = "Challenge3 The1 Weekly2" +## Output: "The Weekly Challenge" +# +############################################################ +## +## discussion +## +############################################################ +# +# Split $str into words, then split each word into the word position +# and the actual word. Then sort this list by word position and +# add the words to the result. + +use strict; +use warnings; + +sort_string("and2 Raku3 cousins5 Perl1 are4"); +sort_string("guest6 Python1 most4 the3 popular5 is2 language7"); +sort_string("Challenge3 The1 Weekly2"); + +sub sort_string { + my $str = shift; + my @words = split/\s+/, $str; + my $sorted = {}; + print "Input: \"$str\"\n"; + foreach my $word (@words) { + $word =~ s/(\d+)$//; + $sorted->{$1} = $word; + } + my $result = ""; + foreach my $number (sort {$a <=> $b} keys %$sorted) { + $result .= "$sorted->{$number} "; + } + $result =~ s/\s+$//; + print "Output: \"$result\"\n"; +} diff --git a/challenge-278/jeanluc2020/perl/ch-2.pl b/challenge-278/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..5ad13bbcf1 --- /dev/null +++ b/challenge-278/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-278/#TASK2 +# +# Task 2: Reverse Word +# ==================== +# +# You are 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 the $char doesn’t exist then DON'T do +# anything. +# +## Example 1 +## +## Input: $str = "challenge", $char = "e" +## Ouput: "acehllnge" +# +## Example 2 +## +## Input: $str = "programming", $char = "a" +## Ouput: "agoprrmming" +# +## Example 3 +## +## Input: $str = "champion", $char = "b" +## Ouput: "champion" +# +############################################################ +## +## discussion +## +############################################################ +# +# If $str matches $char, we split $str into two pieces at $char, +# then we split the prefix part plus $char into single characters, +# sort those and join them together as the new prefix to which we +# append the postfix part. + +use strict; +use warnings; + +reverse_word("challenge", "e"); +reverse_word("programming", "a"); +reverse_word("champion", "b"); + +sub reverse_word { + my ($str, $char) = @_; + print "Input: \"$str\"\n"; + if($str =~ m/$char/) { + my ($prefix, $postfix) = split /$char/, $str, 2; + $str = join("", sort split //, "$prefix$char") . $postfix; + } + print "Output: \"$str\"\n"; +} |
