diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-01 16:59:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-01 16:59:03 +0100 |
| commit | 201bf838c16e853c4d2242f740e7794583990f55 (patch) | |
| tree | 1557401289c0efcccf69337fdda3906ae6eadfc0 | |
| parent | 49257d99a5acd537cfda0082cba75637b0924b0d (diff) | |
| parent | 3de9361042049d85f8fc4abbe42d2883f00ceab2 (diff) | |
| download | perlweeklychallenge-club-201bf838c16e853c4d2242f740e7794583990f55.tar.gz perlweeklychallenge-club-201bf838c16e853c4d2242f740e7794583990f55.tar.bz2 perlweeklychallenge-club-201bf838c16e853c4d2242f740e7794583990f55.zip | |
Merge pull request #10941 from pjcs00/wk289
Week 289 - Big ones and jelmbud wrods
| -rw-r--r-- | challenge-289/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-289/peter-campbell-smith/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-289/peter-campbell-smith/perl/ch-2.pl | 72 |
3 files changed, 121 insertions, 0 deletions
diff --git a/challenge-289/peter-campbell-smith/blog.txt b/challenge-289/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..3b12d7e0f6 --- /dev/null +++ b/challenge-289/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/289 diff --git a/challenge-289/peter-campbell-smith/perl/ch-1.pl b/challenge-289/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..f772bbf8fc --- /dev/null +++ b/challenge-289/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-09-30 +use utf8; # Week 289 - task 1 - Third maximum +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +third_maximum(1, 2, 3, 4, 4, 5); +third_maximum(1, 2); +third_maximum(5); +third_maximum(7, 7, 7, 7, 7); + +# make longer example +my @ints; +push @ints, int(rand(100)) +for 0 .. 49; +third_maximum(@ints); + +sub third_maximum { + + my (@ints, @max, $j); + + say qq[\nInput: \@ints = (] . join(', ', @_) . ')'; + + # reverse sort @ints + @ints = reverse sort {$a <=> $b} @_; + + # use the stated rules to find the first three distinct numbers + for $j (@ints) { + if (not $max[1]) { + $max[1] = $j; + } elsif (not $max[2] and $j != $max[1]) { + $max[2] = $j; + } elsif (not $max[3] and $max[2] and $j != $max[2]) { + $max[3] = $j; + last; + } + } + + # output + if ($max[3]) { + say qq[Output: \$max1 = $max[1], \$max2 = $max[2], \$max3 = $max[3]]; + } else { + say qq[Output: \$max = ] . ($max[2] ? ($max[2] > $max[1] ? $max[2] : $max[1]) : $max[1]); + } +} diff --git a/challenge-289/peter-campbell-smith/perl/ch-2.pl b/challenge-289/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..1b3f59c517 --- /dev/null +++ b/challenge-289/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-09-30 +use utf8; # Week 289 - task 2 - Jumbled letters +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +jumbled_letters(qq[The quick brown fox jumps over the lazy dog.]); +jumbled_letters(qq[The X-factor's inventor was Bloggs-Jones, who said 'Hello!']); +jumbled_letters(qq[Psychological Abstracts contains nonevaluative abstracts of literature in psychology and related disciplines, grouped into 22 major classification categories]); +jumbled_letters(qq[Deoxyribonucleic acid, mucopolysaccharides and propan-2-ol are organic chemicals.]); +jumbled_letters(qq[Das Mädchen möchte die Straẞe früh überqueren.]); +jumbled_letters(qq[L'accent circonflexe va disparaître des manuels scolaires à la rentrée: que s'est-il passé, et qu'en pense le ministre de l'Éducation nationale Najat Vallaud Belkacem?]); +jumbled_letters(qq[Η γρήγορη καφετιά αλεπού πηδά πάνω από το τεμπέλικο σκυλί]); +jumbled_letters(qq[Быстрая бурая лиса перепрыгивает через ленивую собаку.]); + +sub jumbled_letters { + + my ($str, $before, $rest, $middle, $after, $one, $two, $x, $letters, $count, $length, $word, $lm, $s, $m, $jumbled); + + $str = $_[0] . ' '; + $jumbled = ''; + + # loop over 'words' + while ($str =~ m|([^\s]*)\s+|gi) { + + # split word into $before, $middle and $after + $word = $1; + if ($word =~ m|\w| and length($word) >= 4) { + ($before, $rest) = $word =~ m|([^\w]*\w)(.*)|; + ($middle, $after) = $rest =~ m|(.*?)(\w[^\w]*)$|; + + # put just the letters (\w) into letters + $lm = length($middle); + $letters = $middle; + $letters =~ s|[^\w]||g; + $count = length($letters); + + # swap letters around randomly lots of times + if ($count > 1) { + for (0 .. $count + rand(7)) { + do { + $one = int(rand($count)); + $two = int(rand($count)); + } until $one != $two; + $x = substr($letters, $one, 1); + substr($letters, $one, 1) = substr($letters, $two, 1); + substr($letters, $two, 1) = $x; + } + + # now put the jumbled letters in place of the originals + $s = 0; + for $m (0 .. length($middle) - 1) { + if (substr($middle, $m, 1) =~ m|\w|) { + substr($middle, $m, 1) = substr($letters, $s ++, 1); + } + } + } + + # reassemble the word + $word = $before . $middle . $after; + } + + # and add it to the jubled output + $jumbled .= $word . ' '; + } + + say qq[\nInput: $str]; + say qq[Output: $jumbled]; +} |
