What general tips do you have for golfing in Keg? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Keg (e.g. "remove comments" is not an answer).

Please post one tip per answer.

  • 2
    \$\begingroup\$ It's not a great idea to accept an answer for a tips question \$\endgroup\$ – Jo King Oct 15 '19 at 3:54

Brackets autocomplete

This is my biggest tip for anyone golfing in Keg: when writing programs, brackets used in loops or if-statements are automatically balanced if there are unclosed brackets.

For example:


Can be shortened to:




Can be shortened to:


Use Unicode characters when possible

In Keg, you can represent large constants using simply Unicode characters. Sometimes this is really hard to do, sometimes it is easy and can lead to creative solutions. And there is almost nothing to say about this; this is a canonical feature for golfed Keg programs.

  • \$\begingroup\$ With the SBCS this no longer applies \$\endgroup\$ – Jo King Nov 5 '19 at 2:00
  • \$\begingroup\$ Just make sure you clarify that you are using an old version of Keg if you are using this tip. \$\endgroup\$ – user85052 Nov 5 '19 at 2:01

Using the ; operator

The ; operator in Keg can be very powerful. For example for building NOT gates for if statements;


Can be golfed into:


Use Functions where possible

Functions are defined using @ and store code that would potentially be repeated many times.


There's A Dictionary!

Like other golfing languages, Keg has a built-in ~60.5k word (~6.05 times better than 05AB1E's) dictionary that is available to be used. There are 6 ways to access the goodness that is words.txt:

  • Standard Spaceless Strings (Done with backticks)
  • Standard Spaceful Strings (Done with )
  • SCC-Only Spaceless Strings (Done with )
  • SCC-Only Spaceful Strings (Done with )
  • Special Spaceless Strings (Done with )
  • Special Spaceful Strings (Done with «)

Note that when the terms spaceful and spaceless are used, they refer to whether or not the string will place spaces between string compression codes when expanding.

I feel like I should take the time to explain what String Compression Codes are:

Every word within the Keg dictionary has a two byte code associated with it. These range from 00 through to ↫↫. When the uncompressor sees a string, it will go through and expand each SCC with it's corresponding word from the dictionary (for example, H% is Hello, is World and is Good). Even though all of the examples had uppercase letters, there are words starting with lower case letters.

Obtaining SCCs

The best and easiest way to find out what the required SCCs for programs is to navigate to uncompress.py and run the program (preferably in some sort of terminal/something that isn't IDLE). "But that's a library, not a user-friendly program!", I hear you say. Well, when run by a user as a program, it will branch into if __name__ == "__main__" and run a helpful session for golfing. If a word is in the dictionary, the corresponding SCC will be printed (single character SCCs start with 0), otherwise, -1 will be printed. Here's an example session:

Enter a search term: Hello
H% [43, 66]

Enter a search term: world
⬥ [188]

Enter a search term: Keg    

Enter a search term: keg    
←z [130, 35]

Enter a search term: Um    

Enter a search term: Keg   

Enter a search term: isn't   
4🄂 [4, 239]

Enter a search term: in   

Enter a search term: the  
0 [0]

Enter a search term: dictionary  
V¿ [57, 88]

Don't mind the fact that Keg isn't in the dictionary.

  • \$\begingroup\$ I feel like the amount of words in the dictionary does not mean it is a good dictionary. The extra words occur in strings less frequently and will be used less (they will be too space-consuming too). \$\endgroup\$ – user85052 Oct 14 '19 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy