The weekly challenge 289 - Task 2: Jumbled Letters

 1 #!/usr/bin/env perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-289/#TASK2
 3 #
 4 # Task 2: Jumbled Letters
 5 # =======================
 6 #
 7 # An Internet legend dating back to at least 2001 goes something like this:
 8 #
 9 ##    Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in
10 ##    waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht
11 ##    the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl
12 ##    mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn
13 ##    mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.
14 #
15 # This supposed Cambridge research is unfortunately an urban legend. However,
16 # the effect has been studied. For example—and with a title that probably made
17 # the journal’s editor a little nervous—Raeding wrods with jubmled lettres:
18 # there is a cost by Rayner, White, et. al. looked at reading speed and
19 # comprehension of jumbled text.
20 #
21 # Your task is to write a program that takes English text as its input and
22 # outputs a jumbled version as follows:
23 #
24 # 1. The first and last letter of every word must stay the same
25 # 2. The remaining letters in the word are scrambled in a random order (if that
26 #     happens to be the original order, that is OK).
27 # 3. Whitespace, punctuation, and capitalization must stay the same
28 # 4. The order of words does not change, only the letters inside the word
29 #
30 # So, for example, “Perl” could become “Prel”, or stay as “Perl,” but it could
31 # not become “Pelr” or “lreP”.
32 #
33 # I don’t know if this effect has been studied in other languages besides
34 # English, but please consider sharing your results if you try!
35 #
36 ############################################################
37 ##
38 ## discussion
39 ##
40 ############################################################
41 #
42 # We replace all words with a randomized version of it by splitting
43 # each word into its first character, the middle part and the last
44 # character, of which we shuffle the middle part. We use the shuffle
45 # function from List::Util to do the actual randomizing, so the actual
46 # work is running s///eg on the input (e allows to call a function on
47 # the matched parts), and the randomize() function simply splits the
48 # middle part into individual characters, randomizes them and joins
49 # them together again.
50 
51 use strict;
52 use warnings;
53 use List::Util qw(shuffle);
54 
55 jumbled_letters("Perl");
56 jumbled_letters("Banane");
57 jumbled_letters("Motoröl");
58 jumbled_letters("The weekly challenge");
59 jumbled_letters("This supposed Cambridge research is unfortunately an urban legend. However, the effect has been studied.");
60 
61 sub jumbled_letters {
62    my $text = shift;
63    print "Input: $text\n";
64    $text =~ s/\b(\w)(\w*)(\w)\b/randomize($1, $2, $3)/eg;
65    print "Output: $text\n";
66 }
67 
68 sub randomize {
69    my ($x, $y, $z) = @_;
70    return $x . join("", shuffle(split//, $y)) . $z;
71 }
72