python 水平垂直

完成word_puzzle(horizontal_str, vertical_str)函数,它接受两个字符串参数horizontal_str和vertical_str。该函数检查这两个字符串是否有一个相同的字符,并将它们打印出来,以便它们在这个共享字符处相交。

如果两个字符串有一个以上的共同字符,则使用第一个共享字符。这可以通过以下算法检测:

遍历horizontal_str字符串中的每个字符。
如果vertical_str字符串中存在该字符,则使用该字符的第一个出现的字符作为共享字符。
如果两个字符串没有相同的字符,该函数应该打印以下消息:" the two string do not intersection "。

您可以假设两个参数字符串至少有一个字符长,并且只包含小写字母字符。下面显示了调用该函数的三个示例。

img


def word_puzzle(horizontal_str, vertical_str):
    crossindex=-1
    blanknum=0
    for c in horizontal_str:
        crossindex=vertical_str.find(c)
        if crossindex!=-1:
            break
        else:
            blanknum+=1
    if crossindex==-1:
        print('the two string do not intersection')
    else:
        for i in range(len(vertical_str)):
            if i==crossindex:
                print(horizontal_str)
            else:
                print(' '*blanknum+vertical_str[i])
                
word_puzzle('hello','world')
word_puzzle('colony','yummy')
word_puzzle('zeo','yummy')
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632