Average of two strings in alphabetical/lexicographical order

前端 未结 8 1796
南方客
南方客 2021-02-15 17:38

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

8条回答
  •  别那么骄傲
    2021-02-15 18:14

    If you mean the alphabetically, simply use FogleBird's algorithm but reverse the parameters and the result!

    >>> print average('cat'[::-1], 'doggie'[::-1])[::-1]
    cumdec
    

    or rewriting average like so

    >>> def average(a, b):
    ...     a = debase(a[::-1])
    ...     b = debase(b[::-1])
    ...     return enbase((a + b) / 2)[::-1]
    ... 
    >>> print average('cat', 'doggie')
    cumdec
    >>> print average('google', 'microsoft') 
    jlvymlupj
    >>> print average('microsoft', 'google') 
    jlvymlupj
    

提交回复
热议问题