Memoizable Implementation in JS & Python