GlobalLogic interview question

Python function what it does and how to optimize it