rearrange python solution of q022