Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Shorten this one-liner!

by lunix (Novice)
on Aug 26, 2018 at 15:22 UTC ( [id://1221134]=note: print w/replies, xml ) Need Help??


in reply to Re: Shorten this one-liner!
in thread Shorten this one-liner!

Wow! Very interesting reading, you have provided, I like it much.
This task (printing the word, if it contains "a" and then the occurrences of "e") came up years ago in python programming class and some could't do it. I was just trying to show them how uncomplicated it was by taking my perl code and turning it into a one-liner. Guess I was trying to be cool. And that was it, it was saved on my hard drive and forgotten. Now, years later I found the one-liner, which looked like (obviously with different words):

perl -e '$c=0;@s=("anyone","cancel","declare","perlmonks");foreach(@s){if($s[$c]=~/a/){$n=()=$s[$c]=~/e/g;print"$s[$c]: $n\n";}$c++;}'
(135 strokes)

And I had to laugh. This didn't contain any really "perl-y" tricks (except for the regexp maybe), it was my original program without any whitespaces, and shorter variables. So in a few hours I got it down to

perl -e 'for('anyone','cancel','declare','perlmonks'){$i=()=/e/g;print"$_: $i\n"if(/a/);}'
(91 strokes)

And here I got stuck. But I enjoyed getting it shorter, and I knew it could be even shorter. So I posted it here, to see how far I was from the shortest solution. Now we have

perl -E'/a/&&say"$_: ".y/e//for anyone,cancel,declare,perlmonks'
(65 strokes)

And it might get shorter. I don't know.
Also, this isn't really a competition, more like, just me trying to improve my skills. I was curious how differently others might approach it.

PS. I never heard about code golf before, but thanks for introducing me to it, I might get into it with time :)

Regards, lunix

Replies are listed 'Best First'.
Re^3: Shorten this one-liner!
by Marshall (Canon) on Aug 28, 2018 at 00:51 UTC
    To win at normal golf, you have to have the minimum number of strokes for the course.
    Perl "Golf" is similar, the shortest code that does the job (fewest key stokes) wins!

    This Perl golf stuff is meaningless aside from an interesting puzzle.
    I recommend getting solid with your Perl coding skills before writing one-liners.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (1)
As of 2024-04-18 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found