Since we have topics for this in C++ and Haskell, I thought maybe I’d start one in javascript.
Most of my personal tricks are general tricks lifted more or less directly from this great stackexchange thread, but I’ve come up with some cool ones, mostly specific to codingame.
// disclaimer : I jot down everything I can come up with, some will be pretty generic ,some more specific. Please feel free to elaborate, or tell me if some of these realy belong into a more generic one //
1.General tips
consider this fairly standard, beautifully presented js init code :
var array1 = readline().split(' '),
array2 = readline().split(' '),
somenumber = parseInt(array1[0]), someConstant = 0;
- usuals appy here : one letter vars, no
;
, … you know the drill - get rid of var, var is for the weak (and when you’re, you know, writing anything beyond golf)
- CG doesn’t discard blank spaces anymore, so compress that baby! no indent, less newlines, as few spaces as possible
- use implicit typecasting as much as possible! my favorite is
+a
instead ofparseInt(a)
but the stackexchange I linked has many good ones. - functions are variables too, alias the §h|t out of them! for example
readline
is very long soR=readline
saves you loads of bytes, from the second call! for methods, store their name :c='charCodeAt',a=S1[c](0),b=S2[c](0),...
- while you’re at it, cram as much as you can into unused function args, so
a=0,b=readline()
becomesb=readline(a=0)
for a whole one byte saved, but much more if you get creative.
With just those in mind, the previous code can be reduced to r=readline,(x=>r().split(' '),B=f(A=f(C=0)),N=+A[0]
which is much much shorter and an horrible nightmare for anyone that has maintained code they didn’t write…
2. take advantage of having an up-to date js environment : EC6 goodness!
- destructuring assignment :
[a,b,,d]=readline().split() // you can even skip values if you want!
- lambdas, lambdas everywhere :
someArray.map(x=>+x) //converts all values to numeric!
(but really you shouldn’t have so many+
es that you need this one -
array.revert
,array.reduce
,Array.every
,array.map
are here for you, use them! - there are probably good tricks using comprehensions like
A=[for(x of readline().split(' '))+x]
but I haven’t come up with any for now. feel free to comment with some
3. a well written for
trumps most while
loops as seen in the first answer here
I use this one on almost every golf challenge in CG.
4. the ternary operator is almost like black magic or catnip
you probably know by now that a well written ternary can replace a if/else
structure, but the fact that it also returns a value can make for very, very interesting scenarios.
- for example, the smallest of 4 numbers is
result=(x=(a<b?a:b))<(y=(c<d?c:d))?x:y
(not the most useful I know) - but also you can discriminate what to send a function like `print(values[0]?values.reduce(…):“0”)
5. more tricks I’ve come up with
- discriminate between strings via the char position accessor (or “one-letter-long substring”): for example if you want to know if S is “MyAlly” instead of “MyEnnemy” you could use
S=="MyAlly"
, but we’re golfing here, so think moreS[3]=="A"
, or even better :!S[6]
- know about, and distort, truthiness! for example be aware that -1 and “0” are truthy values. so
- the quickest way to test if a string
s
containing a number (as you will often have) is!+s
-
!!array.indexOf(thing)
will not tell you that thing is somewhere in array, but!~array.indexOf(thing)
will (cause ~-1 is 0, which I find weird)
- the quickest way to test if a string
- use and abuse shortcircuiting!
- for defaulting
variable=S.match(/regexp/) || S=defaultCodeNotCalledIfMatch();
- for branching `for(a in readline.split(’ '))+a&&print(a+“is not zer0!”);
- for defaulting
6. please feel free to elaborate
I’m desperate to find ways to reduce the weight of my input loop (readline().split(’ ').map or sort or reduce). If you know about tricks, tell meeeeee!