I’m looking for an algorithm or function that is able to map a string to a number in such way that the resulting values correspond the lexicographic ordering of strings. Example:
"book" -> 50000
"car" -> 60000
"card" -> 65000
"a longer string" -> 15000
"another long string" -> 15500
"awesome" -> 16000
As a function it should be something like: f(x) = y, so that for any x1 < x2 => f(x1) < f(x2), where x is an arbitrary string and y is a number.
If the input set of x is finite, then I could always do a sort and assign the proper values, but I’m looking for something generic for an unlimited input set for x.
If you require that
fmap to integers this is impossible.Suppose that there is such a map
f. Consider the stringsa,aa,aaa, etc. Consider the valuesf(a),f(aa),f(aaa), etc. As we require thatf(a) < f(aa) < f(aaa) < ...we see thatf(a_n)tends to infinity asntends to infinity; here I am using the obvious notation thata_nis the characterarepeatedntimes. Now consider the stringb. We require thatf(a_n) < f(b)for alln. Butf(b)is some finite integer and we just showed thatf(a_n)goes to infinity. We have a contradiction. No such map is possible.Maybe you could tell us what you need this for? This is fairly abstract and we might be able to suggest something more suitable. Further, don’t necessarily worry about solving “it” generally. YAGNI and all that.