Conversion of words into numbers and vice versa [closed]

2

Is there any way to turn entire words into unique numbers so that I can do the reverse process if necessary? As if it were a unique identifier of that word, type

Palavra -> Numero -> Palavra
oi -> 09368 -> oi
    
asked by anonymous 10.05.2016 / 19:36

1 answer

4
  

I will keep this answer here because it can help someone, the question was better clarified in the comments and the problem was another. But the other can not be solved without having criteria to define it, which can be complicated even if you need a single number conversion. Even if it is guaranteed that there can only be words (therefore short), the amount of combinations to avoid repetition is so great that it is better to leave the text even.

I imagine what you want to be the associative array as this feature is best known in PHP:

$palavras = array(
    "oi" => 09368,
    "tchau"   => 01234,
    "palavra" => 34986,
    "abobrinha" => 72494);
$codigo = $palavras["oi"]; //busca rápida
$palavra = array_search(01234, $palavras); //busca lenta

The first thing to do with hash functions , is very fast (O 1 complexity)). The value search is slow (complexity O (N)). It is possible to get complexity O (logN) that is close to O (1) if the array is guaranteed with the ordered values. There you can do a binary search.

It is still possible, if memory is not an impediment, to have a second array with the values inverted. There you can search O (1) in both. So:

$palavras = array(
    09368 => "oi",
    01234 => "tchau",
    34986 => "palavra",
    72494 => "abobrinha");

I kept the zeros not significant, but obviously they are not necessary

    
10.05.2016 / 20:02