Average of two strings in alphabetical/lexicographical order

前端 未结 8 2026
南方客
南方客 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条回答
  •  萌比男神i
    2021-02-15 18:28

    import math
    def avg(str1,str2):
        y = ''
        s = 'abcdefghijklmnopqrstuvwxyz'
        for i in range(len(str1)):
            x = s.index(str2[i])+s.index(str1[i])
            x = math.floor(x/2)
            y += s[x]
        return y
    
    print(avg('z','a')) # m
    print(avg('aa','az')) # am
    print(avg('cat','dog')) # chm
    

    Still working on strings with different lengths... any ideas?

提交回复
热议问题