add functional python solution without global state