Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Homework Golf

by Util (Priest)
on Dec 05, 2013 at 02:56 UTC ( [id://1065697]=note: print w/replies, xml ) Need Help??


in reply to Homework Golf

Perl 6 version:

perl6 -n -e '.say if![+] -1,.uc.ords X-64'

Tricks used: Changed `sum(...) == 65` to `!sum(-65,...)`. The `-65` became `-1` due to the `X- 64`.

Note that Perl 6 requires whitespace in more places than Perl 5 does. Only two spaces were removed from my original version: `if !` and `X- 64`.

Explanation:

.meth
Method call. Operates on $_ when no object precedes the dot.
[+]
[op] is "reduce" metaoperator. Here, it returns the sum.
.uc
Uppercase
.ords
List of ordinal values for every char in string.
X-
Xop is "cross" metaoperator. Here, it subtracts 64 from list on left.
64
Difference of ord('A') and cypher value `1`.
,
Used here to avoid `== 65`; Instead, add -65 to the sum and test for zero.
-1
Difference of -65 and 64. The -1 gets changed to -65 by X- .

Replies are listed 'Best First'.
Re^2: Homework Golf
by McD (Chaplain) on Dec 05, 2013 at 20:06 UTC
    Nicely played, bonus points for such a complete explanation, and top marks for showcasing Perl6 native goodies. Thank you!
Re^2: Homework Golf
by raiph (Deacon) on Dec 05, 2013 at 21:04 UTC
    »»» This post is about the immature Perl 6, not the rock solid Perl 5 «««

    So (thus far) P6 "wins" (is shortest)?

    Even the obvious ungolfed P6 solution is nicely readable -- would be so even for a Perl 6 novice -- and is still just 32 chars inside the quotes:

    perl6 -n -e '.say if 65 == [+] .uc.ords X- 64'

      Technically, yes, the P6 is shortest and should win, but it seems a bit unsporting since the implicit contest was P5.

      Still, as an exhibition entry, I'd give it full marks for such an outstanding performance and such readable code. (Where "readable" is, um, relative. Kind of a tough call in this thread...)

      It's inspired me to go do this year's Perl6 Advent Calendar and spend another December trying to cozy up to the next generation of Perl!

        »»» This post is about the immature Perl 6, not the rock solid Perl 5 «««

        Hi McD,

        it seems a bit unsporting

        As in Util's entry was unsporting? Or my comment?

        the implicit contest was P5

        If the implicit aspect you're speaking of is a general P6-isn't-Perl sentiment, well, I don't subscribe to that. The Meditations section is explicitly inclusive of Perl 6 -- "share or discuss opinions on ... Perl 6". And voting, especially the absence of "significant downvotes", appears to be consistently supportive of reasonable P6 posts.

        That said, I don't want to be the source of undue or unproductive provocation. I apologize if I'm still missing something and ask that you make it explicit for me so I can watch for it in the future.

        I'd give it full marks for such an outstanding performance and such readable code. (Where "readable" is, um, relative. Kind of a tough call in this thread...)

        Yeah, Perl++, P6++, Util++. "I don't know Perl and I can get the gist of it" is from a comment about readability of the P6 solution. (The commenter also notes that their python equivalent "falters a little here because its functional syntax is not the best".)

        It's inspired me to go do this year's Perl6 Advent Calendar and spend another December trying to cozy up to the next generation of Perl!

        :) I repeated my P6 disclaimer block at the start of this comment. Imo P6 is still not ready for most non-contributors and even some who wish to contribute. You will likely become very frustrated if all you want is something that works and you may even become frustrated if all you want to do is contribute. However, if that hasn't put you off then get ready for a lot of fun. :)

Log In?
Username:
Password:

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

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

    No recent polls found