Scoring algorithm in Python