Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: A little golfing challenge: replace digits by random letters (updated) -- oneliner

by Discipulus (Canon)
on Feb 10, 2019 at 13:49 UTC ( [id://1229719]=note: print w/replies, xml ) Need Help??


in reply to A little golfing challenge: replace digits by random letters

Hello haukex,

have you decided to consume all our spare times!?! ;)

Not golfed but maybe of interest

perl '-M0;map{$h{$_}++}97..122;@a=map{chr$_}keys%h;$"=""' -pe 'eval"tr +/0-9/@a[0..9]/"' input.txt
WebPerl link (requires haukex ;)

L*

UPDATE see it running

perl '-M0;$h{$_}++for 97..122;$"=""' -pe 's/(\d)/(map{chr$_}keys%h)[$1]/eg' input.txt

removing unneeded assignemnts..

perl '-M0;$h{$_}++for 97..122' -pe 's/(\d)/(map{chr$_}keys%h)[$1]/eg' input.txt

or even..

perl '-M0;@h{97..122}=1x125' -pe 's/(\d)/(map{chr$_}keys%h)[$1]/eg' input.txt

...

perl '-M0;@h{97..122}=1x125'-pe 's/\d/(map{chr$_}keys%h)[$&]/eg' input.txt

...

perl '-M0;@h{97..122}=1x125'-pe 's/\d/chr((keys%h)[$&])/eg' input.txt

...

perl '-M0;undef@h{97..122}'-pe 's/\d/chr((keys%h)[$&])/eg' input.txt

..stop!

perl '-M0;@h{97..122}=1'-pe 's/\d/chr((keys%h)[$&])/eg' input.txt

L*

congrats Eily for the below reduction!

another variation, not highly random..

perl -pe 's/\d/(a..z)[$&-(split"",time)[-1]]/eg' input.txt

or.. (shorter than Eily's one ;)

perl -pe 's|\d|(a..z)[$&-time%7]|eg' input.txt

after haukex's suggestion:

perl -pe 's|\d|(a..z)[$&-$^T%7]|eg' input.txt

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: A little golfing challenge: replace digits by random letters (updated) -- oneliner
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: A little golfing challenge: replace digits by random letters (updated) -- oneliner
by Eily (Monsignor) on Feb 10, 2019 at 20:24 UTC

    That's already impressive, but why bother with chr in the first place? perl '-M0;@h{+a..z}=1'-pe 's/\d/(keys%h)[$&]/eg' input.txt works just as fine.

    Edit: this can still be reduced a little:

    perl '-M0;@h{a..z}=1'-pe 's/\d/(%h)[$&*2]/eg' input.txt

Re^2: A little golfing challenge: replace digits by random letters (updated) -- oneliner
by Eily (Monsignor) on Feb 11, 2019 at 14:23 UTC

    or.. (shorter than Eily's one ;)

    perl -pe 's|\d|(a..z)[$&-time%7]|eg' input.txt
    That's nearly always correct (if you ignore the fact that it's a shift, not a shuffle). You'll get different values if the lines are not treated in the same second. This would happen if the first line is handled at 9:59:59.9999 and the second at 10:00:00.0000 (add the proper number of digits). Or if you are working on a piped input from a slow program, or the input is big enough. This can be demonstrated by adding a sleep.

    I realized I could shorten the other version (I don't feel like calling it mine when I just took your idea and rewrote it :P), but it's still longer than your latest proposition:

    perl -pe '@h{a..z}=1;s/\d/(%h)[$&*2]/eg' input.txt

    Because since this is golfing, why would you bother doing things properly/efficiently? :P

      You'll get different values if the lines are not treated in the same second.

      I had already /msg'd Discipulus about this - the time can be replaced by $^T :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found