Home > Software design >  How to convert a char array to a number for sorting?
How to convert a char array to a number for sorting?

Time:05-01

Let's say I have three words I want to sort:

  • hello
  • goodbye
  • alex

And alphabetically it'd be sorted ascendingly as ["alex", "goodbye", "hello"]. Is there a way to convert the string (limited to 100 characters)? For example, if I have:

['a', 'l', 'e', 'x']

And I use ord to get the code for each list element:

[1, 12, 5, 24]

How would I create a number from that, that would also be smaller than, say, goodbye?

CodePudding user response:

Use:

from operator import mul, pow
from itertools import repeat

LIMIT = 100
char_vals = [1, 12, 5, 24]
sum(map(mul, char_vals, map(pow, repeat(27), range(LIMIT - 1, -1, -1))))

Since you've bounded the length of the strings, you can think of each word as a base-27 number. 0 represents that the letter doesn't exist, 1 represents a, 2 represents b, etc.

Then, the numerical value of each word can be computed using the following polynomial:

i_1 * 27**99   i_2 * 27**98   ...

where char_vals = [i_1, i_2, ... i_{len(original_string)}] (i.e. each i is the integer value of the corresponding letter, and is that are past the end of the array correspond to zeroes).

CodePudding user response:

Just use map and a lambda function. Python "ord" doesn't work on lists, just characters.

    # Original list
    words = ["hello", "goodbye", "alex"]
    # Ordinals
    words = list(map(lambda word: ord(word[0]), words))
    # To sort it: 
    words = sorted(words)

CodePudding user response:

If we limit the array to N characters, we could do something like this:

words = ["hello", "goodbye", "alex"]
LETTERS_IN_ALPHABET = 26

def num_from_string(s, max_length=10):
    num = 0
    for i, char in enumerate(s):
        num  = ord(char) * ((LETTERS_IN_ALPHABET 1) ** (max_length-i))
    return num

sorted(words, key=num_from_string)
# ['alex', 'goodbye', 'hello']
  • Related