RYFN
RYFN

Reputation: 3069

Calculate an alphabetic score for a word

How can I generate a numeric score for a string, which I can later user to order things alphabetically?

(I'd like to add objectIds to a redis sorted set based on a name property. This needs a numeric score. My list-of-things might be too big to sort all at once, hence wanting to score each item individually)

Words earlier in an alphabetic list should have a lower score, with 'a' = 0.

My naive approach so far; (letter alphabetic position from Replace a letter with its alphabet position )

function alphaScoreString(inputString) {
    let score = 0

    inputString
        .trim()
        .toLowerCase()
        .split('')
        .map((letter, index) => {
            const letterNumber = parseInt(letter, 36) - 10
            if (letterNumber >= 0) {
                score += letterNumber / (index + 1) 
            }
        })

    return score * 1000
}

This does not work, as

alphaScoreString('bb')
1500
alphaScoreString('bc')
2000
alphaScoreString('bbz')
9833.333333333334

You can see that 'bbz' has a higher score than 'bc', whereas it should be lower, as 'bbz' would come before 'bc' in an alphabetical list.

Upvotes: 2

Views: 317

Answers (2)

Akuha
Akuha

Reputation: 11

Answer writen in python.

char_codex = {'a':0.01, 'b':0.02, 'c':0.03, 'd':0.04, 'e':0.05, 'f':0.06,
              'g':0.07, 'h':0.08, 'i':0.09, 'j':0.10, 'k':0.11, 'l':0.12, 
              'm':0.13, 'n':0.14, 'o':0.15, 'p':0.16, 'q':0.17, 'r':0.18, 
              's':0.19, 't':0.20, 'u':0.21, 'v':0.22, 'w':0.23, 'x':0.24,
              'y':0.25, 'z':0.26}

def alphabetic_score(word):
  bitwiseshift = '1'
  scores = [0.00] * len(word)
  for index, letter in enumerate(word.lower()):
    if index is 0:
      scores[index] = char_codex[letter]
    else:
      bitwiseshift = bitwiseshift+'00'
      scores[index] = char_codex[letter]/int(bitwiseshift)
  return sum(scores)

Upvotes: 1

Donald Koscheka
Donald Koscheka

Reputation: 351

You can convert each character to its unicode (and ensure that every character is 4 digits by padding the string. e.g. "H" = 72 but is padded to 0072: Doing a word by word comparison, you can still determine the 'alphabetical order' of each string:

var instring = "Hello World";
var output = "";
for(i=0; i<instring.length;i++){
  const newchar = String(instring.charCodeAt(i)).padStart(4, '0');
  output = output.concat(newchar)
  console.log(output);
}

Upvotes: 2

Related Questions