This is a great example of "lines of code" being prioritized over the actual program itself. I suppose it's neat that he abused style to shrink a program to three lines, but it's not really meaningful.
I do think the program itself is pretty neat and never would have thought of using the word list for hangman.
In this context, I don't really think this should be down-voted. It sums up why LoC-minimisation (unless, perhaps, we say give a strict character limit per line, 80 say) is fairly meaningless.
That said, I think this is a neat, small, working, fun implementation of Hangman, and using the system dictionary is a great idea.
I think characters are a better count of brevity. Consider the archetypical shell is 80 chars wide, so there exists a direct translation to 'real' lines of code.
Okay, I have an even better challenge for you guys.
Instead of minimizing it to least amount of lines/chars, you have to maximize the clarity of the code such that the rest of us laymen can understand it.
My favorite Apple II periodical "Nibble Magazine" had a regular feature called One & Two Liners, neat little type-in BASIC programs. I learned a lot about programming by trying to decode the techniques. Thanks for sharing.
I am sure Electron magazine (or whatever the UK publication for the Acorn Electron was called) had a "ten liners" section. To be honest I never learned much from it, as I was too young to have a proper grasp of programming, but you could get some neat stuff happening by typing those ten lines.
Fun, thanks! I'm going to try and best it in the J programming language. I am betting it will be 1 or 2, 80 character lines maximum. I'll post when done.
I while ago I wrote a simple Hangman in K, a cousin of J. 6 fairly simple SLoC, and there's still some room for golfing it:
d: ("aardvark";"apple";"peanut";"etc"); / dictionary
w: d[*1?#d]; / pick a word
c: {|/w=/:x}; / correctly guessed
b: {{$[x 0;x 1;"_"]}'+(c[x];w)}; / word with blanks
p: {`0:" > ";*0:`}; / prompt for a letter
{~&/c[x]}{`0:b[x]; x,p[]}/"__"; `0:w,"\n";
Nice. And I am sure in K that you can put all of those lines in one big line, making it a one line solution vs. his 3 line solution. This is why I think the suggestion above about character count is more relevant in this type of 'golfing'. This will give me a start on my J program. However, he does some reaching out for words to the word list found in a local word list. I plan on doing the same, and possibly an internet connection more word lists.
EDIT: Realized words from usr/.. word list, not internet.
Sure, I could certainly compress it into a oneliner. I'm mainly focused on trying to make something simple with few moving parts. To that end I did a little more tinkering. Most pleasantly I was able to remove the conditional:
w: *1?("aardvark";"apple";"peanut";"etc") / pick a word
c: {|/w=/:x} / correctly guessed
b: {("_",'w)@'c x} / word with blanks
p: {`0:" > ";*0:`} / prompt for a letter
{~&/c x}{`0:b x; x,p[]}/"__"; `0:w,"\n";
I like the conclusion to this fun little exercise:
"""To write out all the code and make minor refactorings took around 30 minutes. It took me way longer than that to write this blog post detailing exactly how it works. If I wanted to draw a moral from an exercise I entered into purely for amusement, I would have to repeat the oft-quoted wisdom that code that is readable and understandable is going to save you much more time in the long run."""
26 comments
[ 3.1 ms ] story [ 74.5 ms ] threadI do think the program itself is pretty neat and never would have thought of using the word list for hangman.
That said, I think this is a neat, small, working, fun implementation of Hangman, and using the system dictionary is a great idea.
See my 52 char fizz buzz in js: https://github.com/codeocelot/fizzbuzz_oneliner/
That's a good first stab at it, but it can definitely be smaller, esp. if you're willing to add more lines (which I'm not). Happy hacking!
It takes quite a few more characters, but it was fun.
Instead of minimizing it to least amount of lines/chars, you have to maximize the clarity of the code such that the rest of us laymen can understand it.
GO!!!!
Thanks for doing this write up.
The functional style of the code reminded me of Haskell.
EDIT: Realized words from usr/.. word list, not internet.
"""To write out all the code and make minor refactorings took around 30 minutes. It took me way longer than that to write this blog post detailing exactly how it works. If I wanted to draw a moral from an exercise I entered into purely for amusement, I would have to repeat the oft-quoted wisdom that code that is readable and understandable is going to save you much more time in the long run."""
Except for Arch Linux, apparently. It requires the words package, which then links to the file corresponding to your locale.