What strategies are made use of in addressing code golf troubles?

" Regular" golf vs. code golf:

Both are competitors. Both have a distinct set of regulations, which I'll omit for simpleness. Both have distinct objectives; basically, "make use of less hits/characters than your rivals."

To win suits, sports golf enthusiasts rely upon

  • tools.
    • Some scenarios ask for a sand wedge; others, a 9-iron.
  • strategies.
    • The drive functions much better when your feet have to do with shoulder size apart and also your arms are loosened up.
  • and also approaches.
    • Certain, you could take that straight faster way to the opening ... yet do you actually intend to take the chance of the water threat or sand shelter when those trees remain in the means and also the wind is so solid? It could be far better to walk around the lengthy means.

What do code golf enthusiasts have that's analagous to sports golf enthusiasts' tools, strategies and also approaches?

Example response to get this began: make use of the right club! Pick GolfScript as opposed to C#.

2019-05-04 09:17:04
Source Share
Answers: 3

I would certainly claim that detailed expertise of the syntactical anomalies of your language aid. Below is one I located in Ruby when doing a little bit of code golf :

Instead of

require "sequel"
require "nokogiri"
require "chronic"

You can do something similar to this :

 body_of_your_program if %w{sequel nokogiri chronic}.each{|i| require i}

With this example, you also can write unbelievably intricate Ruby one-liners!

In Ruby and also Perl, you additionally get the magic variables like "$ _" which can be made use of to do all type of magic with strings and also regexes. Is your information not strings? Well, you could intend to transform it right into strings.

Clearly, in C, the preprocessor is your close friend.

2019-05-08 05:00:44

Depending on the Golf Code obstacle, simply start by coding it. No mather the quantity of first personality, it's the very first step. Attempting to code from square one with the least personality can simply make it harder. Afterwards seek where you can maximize your code (usage 1 personality variable, do chainning procedure, and so on ). This component entails understanding the language in which you are doing the Golf Code, the far better you recognize it, the extra you will certainly recognize fine-tune that will certainly conserve you personalities.

Instance of this strategy/technique :

When it comes to the tools, for the majority of Golf Code obstacle, the excellent tools is to make use of a personalized language made simply for the obstacle. Occasionally that language currently exist, occasionally you can simply design it and also code an interpreter of it.

2019-05-08 00:20:08

When I do a Code Golf obstacle, I certainly grab an usual set of layout patterns that certainly bring about much shorter code : this set raises with time as even more code golf obstacles are done or simply during regular use a language.

As an example, if I require to have a step-by-step loop, one point I could do generally is :

for ($i = 1; $i <= 10; $i++) {
  // code


$i = 1;

while ($i < 10) {
  // code


Yet this constantly functions also :

while ($i++<10) {
  // code

Various other points include :

  • Using much shorter variations of typical collection operates (fwrite() as opposed to file_put_contents() )
  • Using variables to duplicate expressions
  • capitalizing on just how a language takes care of brackets and also line breaks; in the majority of code golfs, the result hardly ever calls for an intricate code block extending greater than one line (unless you're making use of a language where whitespace is significant )
  • capitalizing on just how a language takes care of result (<?= much shorter than print or echo )
2019-05-07 18:02:46