diff options
| -rw-r--r-- | challenge-289/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-289/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-289/jeanluc2020/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-289/jeanluc2020/perl/ch-2.pl | 72 |
4 files changed, 136 insertions, 0 deletions
diff --git a/challenge-289/jeanluc2020/blog-1.txt b/challenge-289/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..4fc843fb22 --- /dev/null +++ b/challenge-289/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-289-1.html diff --git a/challenge-289/jeanluc2020/blog-2.txt b/challenge-289/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..30e0220d6f --- /dev/null +++ b/challenge-289/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-289-2.html diff --git a/challenge-289/jeanluc2020/perl/ch-1.pl b/challenge-289/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..9f4b6f0800 --- /dev/null +++ b/challenge-289/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-289/#TASK1 +# +# Task 1: Third Maximum +# ===================== +# +# 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 returns 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. +# +############################################################ +## +## discussion +## +############################################################ +# +# We collect all distinct numbers as keys in a hash table. Then we +# sort these by size. In the end we keep the third maximum, if it +# is defined, otherwise the maximum. + +use strict; +use warnings; + +third_maximum(5, 6, 4, 1); +third_maximum(4, 5); +third_maximum(1, 2, 2, 3); + +sub third_maximum { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $seen; + map { $seen->{$_} = 1; } @ints; + my @sorted_keys = sort { $b <=> $a } keys %$seen; + my $result = $sorted_keys[2] // $sorted_keys[0]; + print "Output: $result\n"; +} diff --git a/challenge-289/jeanluc2020/perl/ch-2.pl b/challenge-289/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ab1c8a7360 --- /dev/null +++ b/challenge-289/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-289/#TASK2 +# +# Task 2: Jumbled Letters +# ======================= +# +# 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”. +# +# I don’t know if this effect has been studied in other languages besides +# English, but please consider sharing your results if you try! +# +############################################################ +## +## discussion +## +############################################################ +# +# We replace all words with a randomized version of it by splitting +# each word into its first character, the middle part and the last +# character, of which we shuffle the middle part. We use the shuffle +# function from List::Util to do the actual randomizing, so the actual +# work is running s///eg on the input (e allows to call a function on +# the matched parts), and the randomize() function simply splits the +# middle part into individual characters, randomizes them and joins +# them together again. + +use strict; +use warnings; +use List::Util qw(shuffle); + +jumbled_letters("Perl"); +jumbled_letters("Banane"); +jumbled_letters("Motoröl"); +jumbled_letters("The weekly challenge"); +jumbled_letters("This supposed Cambridge research is unfortunately an urban legend. However, the effect has been studied."); + +sub jumbled_letters { + my $text = shift; + print "Input: $text\n"; + $text =~ s/\b(\w)(\w*)(\w)\b/randomize($1, $2, $3)/eg; + print "Output: $text\n"; +} + +sub randomize { + my ($x, $y, $z) = @_; + return $x . join("", shuffle(split//, $y)) . $z; +} + |
