We’re rewarding the question askers & reputations are being recalculated! Read more.

New answers tagged

0

Do computation in default arguments This can save on braces if the function can be reduced to one statement. A silly example function(x,y,a=x*x+y*y,b=a+x+y)a*b+a+b


-1

from argparse import ArgumentParser, RawDescriptionHelpFormatter p = ArgumentParser(description=__doc__, formatter_class=RawDescriptionHelpFormatter) p.aa = p.add_argument p.aa('--myswitch', help='help text', action='store_true')


8

Save 3 bytes by assigning document to a variable: (d=document).write`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM.<br>YOU SEE CRAB NICHOLSON.<br>HE HAS NO MITTENS.<br>HIS CLAWS ARE MENACING.<br>OBVIOUS EXITS ARE:<br>BATHROOM, KITCHEN, LIVING ROOM<br>AND CRAB NICHOLSON'S<br>ULTIMATE LAIR OF AWESOMENESS&...


1

The module keyword can be used to shorten module names when used repeatedly. For example: Array.fold ... Seq.iter ... List.map ... can become module A=Array A.fold ... module S=Seq S.iter ... module L=List L.map ... This is more useful for longer programs where module methods are used repeatedly (and must be fully named each time because they have the ...


1

A bunch of tips in no particular order: string is pretty long name. Efficiently, ('').char is same as string.char. Even better results can be achieved if you use it together with a semicolon on variables: a=...; print(a:sub(1, 5)), but some string functions don't take strings as input. Lua has automatic conversions between strings and numbers for most cases,...


2

Use the fall at the end for instructions This is a pretty simple tip but many MarioLANG programs end something like this, ending with Mario falling down: ..... ===== and this can be written instead as this: .... ===. saving two bytes. If your program has an elevator near an end like this you can often improve even further. This: ]!...... =#====== &...


2

Use ‘twice’ and the distributive property Instead of: You is the sum of a big big big big cat a big big big cat. Use: You is twice twice twice the sum of a big cat a cat. This saves 2 bytes per twice since 2 uses of big is 8 bytes (with a space) and twice is only 6 bytes.


1

Brackets auto-complete In the scratchblocks3 visual syntax (backwards-compatible with scratckblocks2 syntax), you can auto-complete the brackets. E.g. The hello, world program: when gf clicked say[hello, world] Can be golfed into: when gf clicked say[hello, world


0

A tiny little tip for challenges without input. You can use , instead of [-], if you need to clear the cell quickly, as most of the interpreters (including the TIO.run one) will set the cell contents to EOF representation being zero. This makes programs a tiny bit unportable, but who cares about it in code golf anyway?


2

Math built-ins in 3.8 In addition to the famous walrus operator, Python 3.8 introduces useful new math features. Modular inverse The modular-power built-in pow(base, exp, mod) can now compute the modular inverse using exp=-1. This requires that base and mod are relatively prime integers. >>> pow(38, -1, 97) 23 >>> 23 * 38 % 97 == 1 True ...


1

Index into an array, use a default value or run code if not in range This could be done using a single character: j. For example, this code would output GolfScript, CJam, Pyth for 0, 1, 2, and Seriously for all other cases: ri"GolfScript CJam Pyth"S/{;"Seriously"}j Compared to: ri3,#"GolfScript CJam Pyth Seriously"S/= Actually used more characters. But ...


0

(gforth-specific) Utilize the separate floating-point stack gforth has a separate stack for floating-point numbers. Even if you're only dealing with integers, offloading some work and storage to the FP stack may result in shorter code overall, either by avoiding explicit stack manipulation or using FP-specific operations not available on the main stack. ...


Top 50 recent answers are included