Golf coding


2024, Jul 07 edited
tags: python coding competition 


The Coding Golf competitions are basically coding challenges, where the aim is to provide a solution to a coding problem while minimizing the number of characters. It targets the ability of the competitors to find tricks that reduce the size of the code, without losing it effectiveness. Hence the name of this challenge, coding golf as similarly to the sport, the aim is to achieve the target in the minimum number of shots.

On websites like Codingame it's possible to solve Coding Golf puzzles in the "Taille de Code" section. In this version of the competition, the coder is presented a coding puzzle, then it must then select a coding language. Now it can start building its coding solution for this puzzle. Once a solution is ready, the competitor can submit its solution via the website interface.

The correctness of the code is then verified, after the submission, as the proposed code is tested against a certain number of test cases. Once the code achieves 100% of correctness on the validation cases, the competitor can enter the leader board of the challenge, by submitting again its code. The code is once again checked against a test set of cases and if validated, the submission of the user in entered in the leader board.

It’s then possible to search for a better (smaller) solution and submit it again. Each time the new proposed code must be tested against the validation and the test cases.
As the size of the answer can be dependent on the language used it's possible to submit answers written in different languages. A leader board allows measuring each combination competitor and language performance as well as finding the best results for each language. Indeed the ranking can be different as even if using the same logic, the solution in JavaScript will probably have a different size compared to the one written in Python.

For illustration, you'll find below a few examples of python coding tricks that can be pretty useful in Code Golf challenges.




Coding golf trick in python : the "shortcut if"



Let say that in the algorithm code of the solution at one point, the code needs to test if a given value x is present in a string s and display a text for example "Found" in case x is x else display "Not Found".

Typical answer would look like this:

# Python code example: basic if test
if x in s:
    print("Found")
else:
    print("Not Found")
Windows notepad tell us that this code is 58 characters long.

The coding golf alternative exploits the following facts:

  • that true/false is equivalent to 1/0 in python;
  • that tuples (and all python iterables like strings, lists) are 0 based indexed.

# Python code example: "Shortcut if" test
("Not Found","Found")[x in s]
The code performs exactly the same test, but with a 29 characters long solution, which represents 50% less characters than the first solution.




Coding golf trick in python : the reversed string



Let say that in the algorithm code of the solution, the code needs to print the reverse a string. The same strings but displayed in reversed order. For example if the string s='Hello' the expected result would be "olleH"

Typical answer would look like this:

# Python code example: basic reverse string
print("".join(list(reversed(s)))
Windows notepad tell us that this code is 32 characters long.

The coding golf alternative exploits the fact that in python strings are indexed sequences of characters so they can be managed like an unmutable array:


# Python code example: alternative reverse string
print(s[::-1])
The code performs exactly the same task, but with a 14 characters long solution, which represents 18 characters less than the first solution.




Coding golf trick in python : a counter-example



Sometimes refactoring code doesn't help making code statements shorter. Let's see an example below. Let say that our algorithm needs to handle a string containing the 26 letters of the alphapet in uppercase. Typical code answer would look generate the list using the chr function that generates characters based on their ASCII code :

# Python code example: generating string containing the upper case alphabet
v1="".join(chr(65+x)for x in range(26))
Note: Upper case alphabet in the ASCII table starts at 65 code.
The output is the following:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

This solution takes 39 characters.

But what about the plain assignment of the desired string ?

# Python code example: upper case alphabet string.
v2='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
The code performs exactly the same task, but with a 31 characters long solution, which represents 8 characters less than the first solution.

The fuel of coding inspiration:music, travel, photography and whatever drives creativity to code.

You might also like