Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Homework Golf (35)

by eyepopslikeamosquito (Chancellor)
on Dec 05, 2013 at 09:38 UTC ( #1065732=note: print w/replies, xml ) Need Help??

in reply to Re: Homework Golf (35)
in thread Homework Golf

On *nix ... one shorter still:
#23456789_123456789_123456789_123456789_1234 perl -nlE'unpack("%c*",$_&"\c_"x30)-65||say' words.txt
Two more strokes can be shaved I believe (untested, no Unix box available right now):
#23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*",$_&~"`"x30or say' words.txt
Update: an alternative using v31:
#23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*",$_&v31 x30or say' words.txt

Replies are listed 'Best First'.
Re^3: Homework Golf (35)
by BrowserUk (Pope) on Dec 05, 2013 at 09:56 UTC

    I (breifly) looked for a replacement for "\c_", but I was trying to xor two bareword chars together to produce chr(31). I found several pairs that worked, but then as ^ has lower precedence than x, I couldn't extend the string cos if you bracket the expression, x produces a list not a string :(. Didn't think of &~'`'. Or vstrings:( )

    Also couldn't see how to ditch the unpack parens.

    As neither of the strings needs escape processing (unlike "\c_"), you can switch the "s <-> 's and that works on windows also:

    C:\test>perl -nlE"65^unpack'%c*',$_&~'`'x30or say" words.txt | wc -l 1279

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Homework Golf (35)
by Grimy (Pilgrim) on Dec 06, 2013 at 09:14 UTC
    There’s a third alternative using uc :
    #23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*","?"x30&uc or say'
    Those spaces around or are annoying... Let's use something else to express conditionnal printing. With -p, x= does the job:
    #23456789_123456789_123456789_123456789_1 perl -ple'$_ x=65==unpack"%c*","?"x30&uc'
    While we're looking at those switches... Why the -l? Removing it and adding ord("\n") (10) to the target (65) shaves another stroke:
    #23456789_123456789_123456789_123456789_ perl -pe'$_ x=75=~unpack"%c*","?"x30&uc'
    There's a much shorter solution using a 6-bit checksum. The problem is that it comes up with a few false positives (words whose value is 1, 129, 193...):
    #23456789_123456789_123456789_1234 perl -ple'$_ x=1==unpack"%6c*",uc' perl -pe'$_ x=11==unpack"%6c*",uc'
    Using a 5-bit checksum removes the need for uc, but brings even more false positives:
    #23456789_123456789_123456789_1 perl -ple'$_ x=1==unpack"%5c*"' perl -pe'$_ x=11==unpack"%5c*"' perl -nlE'1^unpack"%5c*"or say' perl -nE'11^unpack"%5c*"or say'

      I'm pretty sure we have a winner here, at least in the perl5 division. Congrats for making me go look up x= ("Get outta here, that's legal? So it is!") and for the brilliant insight that lets you shave off the -l. +10 internets to Grimy!

        Congrats for making me go look up x= ("Get outta here, that's legal? So it is!")
        For Perl historians, note that the "$_ x= boolean expression" idiom is attributed to Larry Wall in the early 1990s. IIRC, this attribution was made by japhy around the time of the first fwp golf games in 2001, for example it is mentioned in this post and elsewhere.

        Also of interest for Perl golf historians is that, as indicated in mtve's golf history book (page 36), Eugene van der Pijll won the second ever fwp golf game, "Get Even", in 2002, by employing Larry's concoction:

        -p $_ x=1&~$.&split$&while aeiouy=~/.?/g
        to solve a similar problem to this game, namely printing all words in a dictionary that satisfy the following constraints: i) are on an even line in the words file; ii) have an even word length; iii) contain an even number of each and every vowel.

        BTW, this game produced some very funny looking solutions, such as this one by Sean McAfee (which, if you squint, you will notice uses Abigail's length horror):

        -ln 1&($.|y|||c|y|a|||y|e|||y|i|||y|o|||y|u|||y|y||)||print

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1065732]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-04-26 17:05 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (486 votes). Check out past polls.