Translated the given C functions to python.