Completed Python solution