for loop to iterate through words

后端 未结 2 1867
我在风中等你
我在风中等你 2021-01-28 20:05

My previous post caused a lot of confusion and it flooded with answers that is not relevant to my questions. (My fault for not clarifying things) I flagged that post and this is

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 20:40

    String slicing will likely make the code much simpler. Here's something to get you started:

    def overlap(left, right):
        for i in reversed(range(len(left))):
                if left[-i:] == right[:i]:
                    break
        return left + right[i:]
    
    for pair in [
        ('keyboard', 'ardjimmy'),
        ('jimmy', 'myolita'),
        ('myolita', 'jimmy'),
    ]:
        left, right = pair
        print pair, '-->', overlap(left, right), overlap(right, left)
    

提交回复
热议问题