Average of two strings in alphabetical/lexicographical order

前端 未结 8 1802
南方客
南方客 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:36

    I haven't programmed in python in a while and this seemed interesting enough to try. Bear with my recursive programming. Too many functional languages look like python.

    def stravg_half(a, ln):
         # If you have a problem it will probably be in here.
         # The floor of the character's value is 0, but you may want something different
         f = 0
         #f = ord('a')
         L = ln - 1
         if 0 == L:
              return ''
         A = ord(a[0])
         return chr(A/2) + stravg_half( a[1:], L)
    
    def stravg_helper(a, b, ln, x):
        L = ln - 1
        A = ord(a[0])
        B = ord(b[0])
        D = (A + B)/2
        if 0 == L:
            if 0 == x:
                 return chr(D)
            # NOTE: The caller of helper makes sure that len(a)>=len(b)
            return chr(D) + stravg_half(a[1:], x)
        return chr(D) + stravg_helper(a[1:], b[1:], L, x)
    
    def stravg(a, b):
        la = len(a)
        lb = len(b)
        if 0 == la:
            if 0 == lb:
                return a # which is empty
            return stravg_half(b, lb)
        if 0 == lb:
            return stravg_half(a, la)
        x = la - lb
        if x > 0:
            return stravg_helper(a, b, lb, x)
        return stravg_helper(b, a, la, -x) # Note the order of the args
    

提交回复
热议问题