Average of two strings in alphabetical/lexicographical order

Posted by Bemmu on Stack Overflow See other posts from Stack Overflow or by Bemmu
Published on 2010-03-24T19:19:43Z Indexed on 2010/03/25 1:23 UTC
Read the original article Hit count: 468

Filed under:
|

Suppose you take the strings 'a' and 'z' and list all the strings that come between them in alphabetical order: ['a','b','c' ... 'x','y','z']. Take the midpoint of this list and you find 'm'. So this is kind of like taking an average of those two strings.

You could extend it to strings with more than one character, for example the midpoint between 'aa' and 'zz' would be found in the middle of the list ['aa', 'ab', 'ac' ... 'zx', 'zy', 'zz'].

Might there be a Python method somewhere that does this? If not, even knowing the name of the algorithm would help.

I began making my own routine that simply goes through both strings and finds midpoint of the first differing letter, which seemed to work great in that 'aa' and 'az' midpoint was 'am', but then it fails on 'cat', 'doggie' midpoint which it thinks is 'c'. I tried Googling for "binary search string midpoint" etc. but without knowing the name of what I am trying to do here I had little luck.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about python