#Small 05AB1E golfing tips
Will expand this with small golfing tips I learned along the way. (Only just started 05AB1E personally.)
D(duplicate) andÐ(triplicate) in combination withs(swap) andŠ(triple-swapa,b,ctoc,a,b) are usually shorter than using©(save in global_variable) and®(push global_variable) inside loops. This saved a byte in this answer of mine, as well as two in this answer of mine.1⁄2(if 1, then increase counter_variable by 1) isn't necessary at the end of aμ(while counter_variable != a, do...), since it's done implicitly (saved a byte in this answer of mine)..Bimplicitly splits on new-lines. This was useful in this answer of mine when we were looking for an alternative for¡(split) while still keeping empty items (NOTE: Solution in the linked answer doesn't work when elements contain trailing spaces after splitting.) - Hopefully a builtin will be added to split but keep empty lines in the future.SÖ(which of the digits of the input-integer can evenly divide the input-integer) will contain the number itself for the digits0(instead of divide-by-zero errors). For example,1053will result in[1,1053,0,1](1053 is divisible by 1 and 3; is not divisible by 5; and gives a division-by-zero error for 0). This was pretty useful in this answer of mine by taking the power of the list, since only1is truthy in 05AB1E and everything else is falsey.SÖPresulting in truthy (1) therefore means an input-integer is evenly divisible by each of its digits.- After seeing
û(palindromize a given string) I was surprised there isn't an is_palindrome builtin. But later on I realized only 2 bytes are needed to accomplish that, which areÂQ(whereÂis bifurcate, which is short forDR: Duplicate & Reverse copy; andQis to check if the top two values on the stack are equal). *has a hidden feature of repeating a certain character/stringnamount of times. I used to uses∍to lengthen repeat a single character b an a amount of times (like this:'xs∍), but apparently*does the same and it doesn't matter if the stack order of the number and string is a,b or b,a: Try it online:'x*. (NOTE: Only works in the Python legacy version, not anymore in the Elixir rewrite.)- When you want to filter a list by multiple things, it's usually cheaper to have multiple loose filters rather than all combined in one. Because when you have two filters you'll need something along the lines of
Ds*(duplicate, swap, multiply to act as logical-AND) vs}ʒ(close first filter, filter again) when you use two filters. For example: in this challenge we have to list all numbers of four digits long, containing at least one0, and with a digit sum equal to9. Using a range[1000,10000]covers the number of four digits long, but then you are left with two more filters. Initially I used44°ŸʒD0åsSO9Q*(14 bytes), but by using two filters a byte can be saved:44°Ÿʒ0å}ʒSO9Q(13 bytes). - When you want to zip with integer
0as filler, you could use0ζ. One issue with this however is that the filler0will become a string"0", so if you later try to sort with mixed strings and integer, this will most likely not give the result you'd want. Here an example of how it will sort the zipped inner lists:0ζ€{. This can be fixed by adding an explicit cast to int (ï) after the zip, and only then sort:0ζï€{. However, using the3⁄4as constant0with the zip-filler, will cause it to remain an integer instead of a string during the zip. So3⁄4ζ€{will save a byte here. This tip was provided by @Mr.Xcoder to save a byte in this answer of mine.
Kevin Cruijssen
- 136.2k
- 14
- 154
- 394