TIL: Code Golf
I originally had a completely different topic picked out for tonight, but when I came across
Code Golf I just had to change it. This is just too neat not to share.
Often when I have some down time at work I’ll spend some time surfing stack overflow looking for some problems to help affirm things I already know, or to learn new things that I didn’t know even existed. Today I happened to see a post on the side bar regarding what seemed like a pretty simple coding problem (I think some kind of for loop), but I didn’t recognize the sub-stack that it belonged to.
After clicking on the link, I was introduced to one of the craziest coding related past times that I’d ever seen: Code Golf.
What the heck is Code Golf
With coding, there’s a sweet spot where code is readable, but not overly verbose nor too short where you can’t understand the intention.
Code Golf takes the later of those two and turns it up a factor of 10.
In Code Golf, a user will offer up a specific task to be accomplished. Let’s say that they want to scan through nested arrays and find every single word that has more than 6 letters in it. The author will list any restrictions for the task and what the expected outcomes should be for various examples. At that point, the puzzle/game is open for everyone to start participating.
The goal of Code Golf is to accomplish the task in as small amount of code as possible. In all of the examples that I looked at, the total number of bytes was what was being used to track. So like in golf: the lowest score is the “winner”.
Unlike different groups that are restricted to a specific language, these puzzles are open to any and all languages, including those that were specifically designed for Code Golf.
Why is this so neat?
As someone that still relatively new to writing code for projects (~2 years now actively coding), the idea that people are able to write the same code that would take me 20 lines to write in less than 20 bytes of code is just mind boggling.
The answers to the problems become (in my eyes) such a jumbled up pile of numbers, letters, and symbols that it becomes completely impractical to read any of what’s written.
Just to give you a little taste, here is an example of how to create an acronym using the V language in 7 bytes. The goal was to look for only capital letters that appeared in a string and make an acronym out of them (i.e. The big Dog is Fat == TDF). Here’s their solution:
And yet the code works! It runs just like it should, solving the tasks that the author requested.
It just amazes me how skilled people are to be able to understand the problem so well that they can write a solution so far abstracted from a readable format. And all as a game too, as none of it would ever pass a code check in any sort of code base. That’s an awesome level of dedication to puzzle solving.
If you’re interested in learning more about Code Golf: