43
7
This is much more advanced than How to randomize letters in a word and Cambridge Transposition because of the rule about which letters may be swapped with which. A simple regex will not suffice here.
It is well known that a text can still be read while the innards of its words have been scrambled, as long as their first and last letters plus their overall outlines remain constant. Given a printable Ascii+Newline text, scramble each word according to these rules:
Scrambling must be (pseudo) random.
A word is a sequence of the Latin characters, A through Z.
Only initial letters will ever be uppercase.
The first and last letters must stay untouched.
When scrambling, only letters within one of the following groups may exchange places:
acemnorsuvwxz
bdfhkl
gpqy
it
j
(stays in place)
Example
Srcmable wrods while psrrnveieg their oeiltnus
It is well known that a txet can still be read while the inrands of its wrods have been srcambled, as long as their fisrt and last letters plus their ovaerll ontliues raemin canstnot. Given a patnirlbe Acsii+Nwnliee txet, samrclbe ecah word anoccdirg to these relus:
Smncrbliag must be (pusedo) rondam.
A wrod is a seqencue of the Latin chreratacs, A thurogh Z.
Only iniital lrttees will eevr be uppcsaere.
The fisrt and lsat lettres must stay uctoenhud.
When sarnclbimg, only letters wihtin one of the fwllnoiog guorps may ecxhange plaecs:
aneusvrowxmcz
bhkfdl
gqpy
it
j
(stays in plcae)Emxaple
t
is supposed to be shorter thanh
although many people do not write it so. – Leaky Nun – 2017-05-10T06:00:52.990@LeakyNun I know, but are you suggest removing
t
from group 2? Or maybe puttingt
in a group 4 withi
? – Adám – 2017-05-10T06:02:16.470The latter would be fine. – Leaky Nun – 2017-05-10T06:08:53.117
may the runtime be theoretically unbounded? (like random tries until something is right) – Display Name – 2017-05-10T09:40:45.090
@SargeBorsch Probably ok if done per word (as it is extremely likely to terminate fast), but not if done on the entire text (as that will probably never terminate). However, I'm not entirely sure I should allow this, as I don't want to trivialize the problem. I'd rather see clever solutions than brute ones. It isn't necessary in this case, as I've written a solution that doesn't. – Adám – 2017-05-10T09:57:13.327
1
printable
/patnirlbe
isn't quite readable. I think thei
/t
swap is to blame. Hmm...paintrlbe
No that didn't help either. It's probably thepr
/pa
swap, then. The outline maintains, but I think that I read "pr" and "pa" as being semantically(?) 1 letter.prtnialbe
Ah yes. That did it. Not sure I can offer a fix to the algorithm though. – Draco18s no longer trusts SE – 2017-05-10T18:20:37.303@Draco18s Well, really there should be more groups,
r
has a unique lower-right vacuum, and there is also the amount of "ink" per character and its width… – Adám – 2017-05-10T18:22:24.373@Adám Too many groups and there wouldn't be valid swaps. :P Character width can be mitigated by only using a monospaced font to read the results in. But yes. – Draco18s no longer trusts SE – 2017-05-10T18:30:07.023