Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

5 of 16
added 821 characters in body
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#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 with s (swap) and Š (triple-swap a,b,c to c,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).
  • .B implicitly 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.
  • (which of the digits of the input-integer can evenly divide the input-integer) will contain the number itself for the digits 0 (instead of divide-by-zero errors). For example, 1053 will 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 only 1 is truthy in 05AB1E and everything else is falsey. SÖP resulting 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 for DR: Duplicate & Reverse copy; and Q is to check if the top two values on the stack are equal).
  • * has a hidden feature of repeating a certain character/string n amount of times. I used to use s∍ 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 one 0, and with a digit sum equal to 9. Using a range [1000,10000] covers the number of four digits long, but then you are left with two more filters. Initially I used 44°Ÿʒ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 0 as filler, you could use . One issue with this however is that the filler 0 will 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 the 3⁄4 as constant 0 with the zip-filler, will cause it to remain an integer instead of a string during the zip. So 3⁄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

AltStyle によって変換されたページ (->オリジナル) /