Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

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)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda