diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-09-30 00:45:22 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-09-30 00:45:22 -0700 |
| commit | 9bedd56645f3a9f87896bdc9f9933ea1c19fce5f (patch) | |
| tree | fe90e326e341b62d1d3a412512eaa0ecc5840223 | |
| parent | 5bfb306153546664c3549d2aafab5a2bed4b1c88 (diff) | |
| download | perlweeklychallenge-club-9bedd56645f3a9f87896bdc9f9933ea1c19fce5f.tar.gz perlweeklychallenge-club-9bedd56645f3a9f87896bdc9f9933ea1c19fce5f.tar.bz2 perlweeklychallenge-club-9bedd56645f3a9f87896bdc9f9933ea1c19fce5f.zip | |
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #289.
| -rwxr-xr-x | challenge-289/robbie-hatley/perl/ch-1.pl | 78 | ||||
| -rwxr-xr-x | challenge-289/robbie-hatley/perl/ch-2.pl | 119 |
2 files changed, 197 insertions, 0 deletions
diff --git a/challenge-289/robbie-hatley/perl/ch-1.pl b/challenge-289/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..f6e59608f6 --- /dev/null +++ b/challenge-289/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 289-1, +written by Robbie Hatley on Sun Sep 29, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 289-1: "Third Maximum" +Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. Write a script to +find the third distinct maximum in the given array. If third +maximum doesn’t exist then return the maximum number. + +Example 1: +Input: @ints = (5, 6, 4, 1) +Output: 4 +The first distinct maximum is 6. +The second distinct maximum is 5. +The third distinct maximum is 4. + +Example 2: +Input: @ints = (4, 5) +Output: 5 +In the given array, the third maximum doesn't exist, therefore +return the maximum. + +Example 3: +Input: @ints = (1, 2, 2, 3) +Output: 1 +The first distinct maximum is 3. +The second distinct maximum is 2. +The third distinct maximum is 1. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +Function "uniq" from CPAN module "List::Util" will be very useful here. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of integers, in proper Perl syntax, like so: +./ch-1.pl '([-3,8,5,-1,9],[])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + +use v5.16; +use List::Util 'uniq'; +sub third_maximum { + my $aref = shift @_; + my @uniq = uniq sort {$b<=>$a} @{$aref}; + if (scalar(@uniq) >= 3) {return $uniq[2];} + elsif (scalar(@uniq) >= 1) {return $uniq[0];} + else {return 'undef';} +} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : ([5, 6, 4, 1],[4, 5],[1, 2, 2, 3]); +# Expected output: 4 5 1 + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + my $third_max = third_maximum($aref); + say ''; + say "Array = (@$aref)"; + say "Third maximum = $third_max" +} diff --git a/challenge-289/robbie-hatley/perl/ch-2.pl b/challenge-289/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..27c7a94455 --- /dev/null +++ b/challenge-289/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,119 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 289-2, +written by Robbie Hatley on Mon Sep 30, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 289-2: "Jumbled Letters" +Submitted by: Ryan Thompson + +An Internet legend dating back to at least 2001 goes something +like this: + +Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t +mttaer in waht oredr the ltteers in a wrod are, the olny +iprmoetnt tihng is taht the frist and lsat ltteer be at the +rghit pclae. The rset can be a toatl mses and you can sitll raed +it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed +ervey lteter by istlef, but the wrod as a wlohe. + +This supposed Cambridge research is unfortunately an urban +legend. However, the effect has been studied. For example — +and with a title that probably made the journal’s editor a little +nervous — "Raeding wrods with jubmled lettres: there is a cost" +by Rayner, White, et. al. looked at reading speed and +comprehension of jumbled text. + +Your task is to write a program that takes English text as its +input and outputs a jumbled version as follows: +The first and last letter of every word must stay the same. +The remaining letters in the word are scrambled in random order. +(if that happens to be the original order, that is OK). +Whitespace, punctuation, and capitalization must stay the same. +Word order does not change, only the letters inside the word. + +So, for example, “Perl” could become “Prel”, or stay as “Perl,” +but it could not become “Pelr” or “lreP”. + +I don’t know if this effect has been studied in other languages +besides English, but please consider sharing your results if you +try! + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +The trick will be to determine where each "word" begins and ends. The regexp metasymbol "\b" should help with +this. Apostrophes will complicate things a bit, but I'll handle those in a second pass through the string. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: + +Input is via a single command-line argument, which must be a single-quoted string, with any apostrophes +escaped as '"'"', in proper Perl syntax, like so: +./ch-2.pl 'She shaved her legs, then she gobbled seven hotdogs, but she didn'"'"'t take a bath.' + +Output is to STDOUT and will be a version of the input string with each word's inner letters scrambled. + +Example #1: +./ch-2.pl 'She shaved her legs, then she gobbled seven hotdogs, but she didn'"'"'t take a bath.' +She shvaed her legs, tehn she gbobled sveen hogotds, but she dnid't take a btah. + +Example #2: +./ch-2.pl 'A version of the input string with each word'"'"'s inner letters scrambled.' +A vsroein of the input srntig wtih ecah wrod's inner ltteres searclbmd. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + +use v5.16; +use utf8; + +# Scramble a cluster of characters: +sub scramble { + my $input = shift @_; + my $output = ''; + while ($input) { + my $ran_idx = int rand length $input; + my $spliced = substr $input, $ran_idx, 1, ''; + $output .= $spliced; + } + $output; +} + +# Jlmbue the "inenr" ltetrs of the wrdos of a srtnig: +sub jumble { + # Get our string: + my $string = shift @_; + + # First, jumble the inner letters of words with no apostrophe: + $string =~ s/\b([A-Za-z])([a-z]+)([a-z])\b/$1.scramble($2).$3/eg; + + # Now, handle words with apostrophes: + $string =~ s/\b([A-Za-z])([a-z]+)('[a-z]+)\b/$1.scramble($2).$3/eg; + + # Return our result: + $string; +} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +if (1 != scalar(@ARGV) ) { + die qq(Error: Must have 1 argument, which must be a single-quoted sentence in\n) + .qq(a language, such as English or French, which uses space-separated words.\n) + .qq(Any apostrophes must be escaped as '"'"' .\n) + .qq(Sample valid inputs:\n) + .qq(./ch-2.pl 'She shaved her legs, then she gobbled seven hotdogs, but she didn'"'"'t take a bath.'\n) + .qq(./ch-2.pl 'A version of the input string with each word'"'"'s inner letters scrambled.'\n); +} +my $string = $ARGV[0]; + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +my $jumbled = jumble($string); +say "$jumbled"; |
