Howdy :)
Update:Approximately 30 seconds after posting this (arg!), I found
this link which talks about this phenomena.
(Apologies in advance if this has been covered previously)
Some people may be aware of the phenomena whereby if you take a phrase and
rearrange the letters in each word - but keep the first and last letters in
place - then the phrase as a whole remains readable. (There is probably a name
for this, but I don't know what it is).
As a novice Perl programmer, I thought it would be a fun exercise to see how I
could take a series of phrases and "scramble" them in this way.
So this is my attempt at it:
#!/usr/bin/perl -w
use strict;
use List::Util qw(shuffle);
while (<DATA>) {
my @words = split;
WORD:
for my $word (@words) {
my $offset = $word =~ /[\.\,]$/ ? 2 : 1;
my @letters = split //, $word;
my $numletters = @letters;
next WORD if $numletters < 4;
$word = join("",
$letters[0],
shuffle(@letters[1 .. $#letters-$offset]),
@letters[-$offset .. -1]
);
}
print "@words\n";
}
__DATA__
Down that path lies madness. On the other hand, the road to hell is pa
+ved with melting snowballs.
Although the Perl Slogan is There's More Than One Way to Do It, I hesi
+tate to make 10 ways to do something.
And don't tell me there isn't one bit of difference between null and s
+pace, because that's exactly how much difference there is.
Randal said it would be tough to do in sed. He didn't say he didn't un
+derstand sed. Randal understands sed quite well. Which is why he uses
+ Perl.
As usual, I'm overstating the case to knock a few neurons loose, but t
+he truth is usually somewhere in the muddle, uh, middle.
Of course, I reserve the right to make wholly stupid changes to Perl i
+f I think they improve the language.
Of course, the output is different on every run - but here is an example:
Dwon that ptah leis msenads. On the oethr hand, the raod to hell is pv
+aed with mlinetg sblwlonas.
Agohutlh the Perl Sgalon is Treeh's More Tahn One Way to Do It, I hasi
+tete to mkae 10 wyas to do snmoiehtg.
And dn'ot tell me there in'st one bit of diferefcne beweetn nlul and s
+cpae, becasue ta'hts elxctay how much drcinefefe tehre is.
Rdanal said it would be tough to do in sed. He dd'nit say he dd'int un
+datenrsd sed. Ranadl uenadnsdrts sed qitue wlel. Wchih is why he uess
+ Prel.
As uausl, I'm osvtneatirg the case to kocnk a few nurnoes lsooe, but t
+he ttruh is uualsly semwerohe in the mdudle, uh, mldide.
Of cusore, I reevsre the rhgit to mkae wohlly sptiud cgeanhs to Perl i
+f I thnik they ivopmre the lgagunae.
For those that are interested, I'd be really keen to see how well this can be
"golfed". The rules are simple:
- Take a single line of text (a phrase - in this case, some Larry Wall quotes) as input.
- Split the phrase into words (on whitespace).
- Skip any words containing less than 4 letters.
- Shuffle the letters in each word, keeping the first and last in place.
- If a "word" ends with a comma/period, keep the comma/period in place as
well as the preceeding letter.
- Reconstruct the phrase with the shuffled words and print it.
Cheers,
--Darren :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.