Re: Homework Golf
by oiskuu (Hermit) on Dec 04, 2013 at 07:19 UTC
|
perl -lnE'unpack("%a*")-65-96*length||say'
45
(Lowercase dictionary.)
perl -lne'65+length()*96-unpack"%a*"or print'
50
perl -lne'65+length()*96-unpack"%a*",lc or print'
Note: I added the say version following Athanasius's example.
Dec 05: x-d the readmore tags. Once more as above, using Abigail's Horror:
#23456789_123456789_123456789_123456789_
perl -lnE'65+96*y///c-unpack"%a*"or say'
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
And now I have to live the rest of my life knowing what that is, so thanks for that.
| [reply] |
|
36
perl -nE'65+length()*96-unpack"%a*",lc or say'
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] |
|
| [reply] |
|
I love this one. Took me ten minutes and a command line to take it apart and grok it. Awesome.
| [reply] |
Re: Homework Golf
by ambrus (Abbot) on Dec 04, 2013 at 07:32 UTC
|
That brings back memories.
Back in 2008 we've had some annoying users here who spent time with this numerology thingy by adding up the letters of a line in the Bible. It was at that time when I wrote a script to add up all lines of the kitty pidgin translation of the Bible translated so far. The most frequent sum turned out to be 442.
| [reply] [d/l] |
Re: Homework Golf
by BrowserUk (Patriarch) on Dec 04, 2013 at 02:39 UTC
|
57
54
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.
| [reply] [d/l] [select] |
|
The second solution has a problem: each chop reduces $_ by one character until $_ is the empty string — which is what gets printed when $n is 65. My fix has 59 characters:
But the first solution can be reduced a little to 54 characters:
#23456789_123456789_123456789_123456789_123456789_1234
perl -nlE"$n=0;map$n+=$_-96,unpack'C*',$_;$n==65&&say" words.txt
:-)
| [reply] [d/l] [select] |
Re: Homework Golf
by atcroft (Abbot) on Dec 04, 2013 at 06:06 UTC
|
I'll admit up front-I'm no golfer, and the way some monks can golf code so seemingly effortlessly still amazes me. Having said that, I was still pleased that I managed to get down to 77 characters (all commands in the form of perl -lne '$code' /usr/share/dict/words, unless otherwise noted. Counts only the code in quotes, so if I mis-counted, please let me know.):
An interesting puzzle, McD-thank you for sharing it.
| [reply] [d/l] [select] |
Re: Homework Golf (35)
by BrowserUk (Patriarch) on Dec 04, 2013 at 23:55 UTC
|
#23456789_123456789_123456789_123456789_123456
# 123456789_123456789_123456789_12345
perl -nlE"unpack('%c*',$_&chr(31)x30)-65||say" words.txt
On *nix, it can be 1 shorter (correct nested 's; thanks choroba): #23456789_123456789_123456789_123456789_12345
# 123456789_123456789_123456789_1234
perl -nlE'unpack("%c*",$_&"\x1f"x30)-65||say' words.txt
And one shorter still: #23456789_123456789_123456789_123456789_12345
# 123456789_123456789_123456789_1234
perl -nlE'unpack("%c*",$_&"\c_"x30)-65||say' words.txt
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.
| [reply] [d/l] [select] |
|
#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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
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'
| [reply] [d/l] [select] |
|
|
|
Wow, I love this.
$_&"\c_"x30
...is about as close to executable line noise as I've seen in a long, long time. Bravo!
If I'm smart, I'll borrow all these tricks, next time I come up with a golf idea. :-)
| [reply] [d/l] |
Re: Homework Golf
by choroba (Cardinal) on Dec 04, 2013 at 11:24 UTC
|
| [reply] |
Re: Homework Golf
by Util (Priest) on Dec 05, 2013 at 02:56 UTC
|
Perl 6 version:
Explanation:
| [reply] [d/l] [select] |
|
Nicely played, bonus points for such a complete explanation, and top marks for showcasing Perl6 native goodies. Thank you!
| [reply] |
|
perl6 -n -e '.say if 65 == [+] .uc.ords X- 64'
| [reply] [d/l] |
|
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!
| [reply] |
|
|
Re: Homework Golf
by Jasper (Chaplain) on Dec 17, 2013 at 14:47 UTC
|
I'm about a week late to this, but in case anyone hasn't posted something exactly like this:
-p $_ x=65==map{97..ord lc}/./g
thanks to user oiskuu for the correction on the 97 | [reply] [d/l] |
|
$_ x=65==map{a..lc}/./g
This 'problem' popped into my head again last week with the thought that, unusually, I'd been an idiot. :) This beats even Grimy's 5 bit match (with the false positives) into a cocked hat. I think I win :D
| [reply] [d/l] |
Re: Homework Golf
by sundialsvc4 (Abbot) on Dec 04, 2013 at 17:27 UTC
|
So they teach programming in second grade now ... or, "is there an app for that?" (Finding words, I mean, not "second grade" ... or, do I?) | [reply] |
|
No, the homework in question was actually simple math homework. Honestly, I don't think it was a great question for second graders, but maybe my irresistible bias towards a brute force answer clouds my judgement?
| [reply] |