Implement nucleus sampling (top-p sampling) from scratch in Python, ensuring efficient token selection and probability normalization.