The function is provided in the attached file.