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.
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.
Sign up to join this communityWhat 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.
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:
(9|0.)
Can be shortened to:
(9|0.
And:
{:1=[1.]}
Can be shortened to:
{:1=[1.
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.
;
operatorThe ;
operator in Keg can be very powerful. For example for building NOT gates for if statements;
11$-[Blah|Blah2]
Can be golfed into:
1;[Blah|Blah2]
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
:
¶
)‘
)“
)„
)«
)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
, c¡
is World
and 5Ƴ
is Good
). Even though all of the examples had uppercase letters, there are words starting with lower case letters.
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
-1
Enter a search term: keg
←z [130, 35]
Enter a search term: Um
-1
Enter a search term: Keg
-1
Enter a search term: isn't
4🄂 [4, 239]
Enter a search term: in
-1
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.
Functions are defined using @
and store code that would potentially be repeated many times.