diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-09-30 08:13:01 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-09-30 08:13:01 +0000 |
| commit | 95c9547954a4eaaccca5e8ce3bfb0549966619ae (patch) | |
| tree | 369e9f5c390703417ae357f8d824453ba8bd4598 | |
| parent | f5953e315fe4ad666cfb5a903dc3fa2b5ce704ee (diff) | |
| download | perlweeklychallenge-club-95c9547954a4eaaccca5e8ce3bfb0549966619ae.tar.gz perlweeklychallenge-club-95c9547954a4eaaccca5e8ce3bfb0549966619ae.tar.bz2 perlweeklychallenge-club-95c9547954a4eaaccca5e8ce3bfb0549966619ae.zip | |
w289 - Task 1 & 2
| -rwxr-xr-x | challenge-289/perlboy1967/perl/ch1.pl | 36 | ||||
| -rwxr-xr-x | challenge-289/perlboy1967/perl/ch2.pl | 57 |
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-289/perlboy1967/perl/ch1.pl b/challenge-289/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..a8d0fc1315 --- /dev/null +++ b/challenge-289/perlboy1967/perl/ch1.pl @@ -0,0 +1,36 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-289#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 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. + +=cut + +use v5.32; +use feature qw(signatures); +no warnings qw(experimental::signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub nThMaximum :prototype(\@$) ($arInts,$n){ + my $n2 = ($#$arInts+1 >= $n ? $n : $#$arInts+1); + (sort { $a <=> $b } @$arInts)[-$n2]; +} + +is(nThMaximum(@{[5,6,4,1]},3),4,'Example 1'); +is(nThMaximum(@{[4,5]},3),4,'Example 2'); +is(nThMaximum(@{[1,2,2,3]},3),2,'Example 3'); +is(nThMaximum(@{[5,3,2]},2),3,'Own example'); + +done_testing; diff --git a/challenge-289/perlboy1967/perl/ch2.pl b/challenge-289/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..73a486a55e --- /dev/null +++ b/challenge-289/perlboy1967/perl/ch2.pl @@ -0,0 +1,57 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-289#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 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: + +1) The first and last letter of every word must stay the same +2) The remaining letters in the word are scrambled in a random order + (if that happens to be the original order, that is OK). +3) Whitespace, punctuation, and capitalization must stay the same +4) The order of words 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”. + +=cut + +use v5.32; +use feature qw(signatures); +no warnings qw(experimental::signatures); +use common::sense; + +use List::Util qw(shuffle); + +sub jumbledLetters ($str) { + $str =~ s{([a-z])([a-z]+)([a-z])}{$1.join('',shuffle split //,$2).$3}geisr; +} + +say jumbledLetters(<<EOT); +According to a research at Cambridge University, it doesn't matter in what order +the letters in a word are, the only important thing is that the first and last +letter be at the right place. The rest can be a total mess and you can still +read it without problem. This is because the human mind does not read every +letter by itsel, but the word as a whole. +EOT |
