Rewriting the code with NumPy