Cheatsheet: Data Structures
    Array / List
    Map / Dict
    Set
    Tuple
    Struct / Data Class
    Stacks
    Other Data Structures
    Collection Conversions
Cheatsheet: Control Flow
Cheatsheet: Class

Polyglot CheatSheet - Math

Updated: 2022-04-25

Sum of Array

JavaScript

Use .reduce():

> a = [1, 2, 3, 4]
[ 1, 2, 3, 4 ]
> a.reduce((x, y) => x + y)
10

The full signature is:

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

However only total and currentValue are required.

Cumulative Sum

JavaScript

> const cumSum = [];
> [1, 2, 3, 4].reduce((a, b, i) => (cumSum[i] = a + b), 0);
> cumSum
[1, 3, 6, 10]

Max / Min / Mean

Hack

Math\max()
Math\min()
Math\mean()

Round

Java

Integer.valueOf(Math.round(value));

Log

Java

These 2 are equivalent:

Math.log1p(x)
Math.log(x+1)

Fibonacci

Python

def fibonacci():
    a, b = 0, 1
    while True:
        yield b
        a, b = b, a + b