1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 289
=========================
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".
I don't know if this effect has been studied in other languages besides English,
but please consider sharing your results if you try!
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, an example is presented: original
text, followed by that same text in a jumbled form.
2. If the command-line contains the "--file" flag followed by a filename, the
text in that file is displayed in jumbled form.
3. If the command-line contains a single string, the text in that string is dis-
played in both its original and jumbled forms.
=end comment
#===============================================================================
my regex LETTER { <[ A..Z a..z ]> };
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 289, Task #2: Jumbled Letters (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Str:D $str #= String to jumble
)
#===============================================================================
{
"Original text:\n$str\n".put;
my Str $jumbled = jumble( $str );
"Jumbled text:\n$jumbled".put;
}
#===============================================================================
multi sub MAIN
(
Str:D :$file where *.IO.f #= Name of file containing text to jumble
)
#===============================================================================
{
my Str $str = $file.IO.slurp;
my Str $jumbled = jumble( $str );
qq[Jumbled text from file "$file":\n\n$jumbled].print;
}
#===============================================================================
multi sub MAIN() # No input: show an example
#===============================================================================
{
"Example:\n".put;
my Str $str = '';
for example-data.lines -> Str $line
{
$str ~= $line.chomp;
}
"Original text:\n$str\n".put;
my Str $jumbled = jumble( $str );
"Jumbled text:\n$jumbled".put;
}
#-------------------------------------------------------------------------------
sub jumble( Str:D $str --> Str:D )
#-------------------------------------------------------------------------------
{
my Str @chunks = $str.split( / (<LETTER>+) /, :v ).map: { ~$_ };
for @chunks -> Str $chunk is rw
{
if $chunk ~~ / ^ (<LETTER>) (<LETTER>+) (<LETTER>) $ /
{
my Str $middle = (~$1).split( '', :skip-empty ).pick( ~$1.chars ).
join: '';
$chunk = ~$0 ~ $middle ~ ~$2;
}
}
return @chunks.join: '';
}
#-------------------------------------------------------------------------------
sub error( Str:D $message )
#-------------------------------------------------------------------------------
{
"ERROR: $message".put;
USAGE();
exit 0;
}
#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
#-------------------------------------------------------------------------------
sub example-data( --> Str:D )
#-------------------------------------------------------------------------------
{
return q:to/END/;
According to a researcher 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 itself, but the word as a
whole.
END
}
################################################################################
|