Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:52:14+00:00 2026-06-14T09:52:14+00:00

I am trying to calculate a score for a word so that it will

  • 0

I am trying to calculate a “score” for a word so that it will be used to determine it’s lexicographical order in a Redis sorted set (words listed in alphabetical order).

Reading this post it says:

How to turn a word into a score?

For instance, if you want to use the first four letters to produce the
score, this is the rule:

score = first-byte-value*(256^3) + second-byte-value*(256^2) +
third-byte-value*(256^1) + fourth-byte-value

Just omit from the sum non existing chars if the word is < 4 chars in
length.

Why this works? You are just considering the bytes as digits of a
radis-256 number 🙂

With this theory I came up with the following code to test whether this would work in a PHP array:

$words = array('abcd', 'hello', 'dogs', 'hiya');
$newWords = array();

foreach ($words as $word) {
    $len = strlen($word);

    if ($len > 4) {
        $len = 4;
    }

    $i = 0;
    $j = $len - 1;
    $score = 0;

    while ($i < $len) {
        $byte = ord($word[$i]);

        if ($j == 0) {
            $score += $byte;
        }
        else {
            $score += $byte * (256 ^ $j);
        }

        $i++;
        $j--;
    }

    $newWords[$score] = $word;
}

ksort($newWords);
print_r($newWords);

However this returns:

Array
(
    [75950] => abcd
    [80858] => hello
    [81124] => dogs
    [85220] => hiya
)

Which is not in alphabetical order.

Can anyone spot the issue (obviously the score calculation is wrong)? I may have mis-understood the post :-/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:52:15+00:00Added an answer on June 14, 2026 at 9:52 am

    I improved the code a bit and changed to use a pow instead

    $words = array('abcd', 'hello', 'dogs', 'hiya');
    $newWords = array(); 
    foreach ($words as $word) {
    
    $len = strlen($word);
    
        if ($len > 4) {
            $len = 4;
        }
    
        $i = 0;
        $j = $len - 1;
        $score = 0;
    
        while ($i < $len) {
            $byte = ord($word[$i]);
            $score += $byte * pow(256, $j);
            $i++;
            $j--;
        }
    
        $newWords[$score] = $word;
    }
    ksort($newWords);
    print_r($newWords);
    

    it does exactly what you expected:

    Array ( [1633837924] => abcd [1685022579] => dogs [1751477356] => hello [1751742817] => hiya )
    

    and you actually used XOR http://www.php.net/manual/en/language.operators.bitwise.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set up a function that will calculate a score for
I'm trying to generate some sql that is used to calculate some final scores
I am trying to create an SQL statement that will essentially calculate points due
I'm trying to calcuate the confidence score that a string appears within a subset
Helllo! I'm trying to build a program that calculates the best score from a
Problem description : I'm trying to define a pl/python aggregator that receives a set
I am trying to calculate a special score. The 'total' and the 'correct' (or
I'm trying to figure out the best way to calculate a value that is
I'm creating a word search and am trying to calculate quality of the generated
I'm trying to calculate the median of a set of values, but I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.