Implement a robust Python generator | Google Interview Question