8-p08_match


def match(s,p):
    if len(p)==0:
        return len(s)==0

    first = p[0]
    if first=='?':
        return len(s) > 0 and match(s[1:],p[1:])
    if first=='*':
        return match(s,p[1:]) or len(s) > 0 and match(s[1:],p)
    return len(s) > 0 and first==s[0] and match(s[1:],p[1:])


def _test_match(s,p,result):
    print('%s,%s,%s,%s'%(s,p,result,match(s,p)))


if __name__=='__main__':
    _test_match('ababaab','a*b',True)
    _test_match('ababaab', '*abab*', True)
    _test_match('ababaab', 'a*a?b', True)

    _test_match('ababaab', 'a*bb', False)
    _test_match('ababaab', 'aabab*', False)
    _test_match('ababaab', 'a*b?b', False)
D:\Anaconda\python.exe D:/AI20/06_codes/deeplearning_20/p08_match.py
ababaab,a*b,True,True
ababaab,*abab*,True,True
ababaab,a*a?b,True,True
ababaab,a*bb,False,False
ababaab,aabab*,False,False
ababaab,a*b?b,False,False

Process finished with exit code 0

发布了88 篇原创文章 · 获赞 2 · 访问量 1311

猜你喜欢

转载自blog.csdn.net/HJZ11/article/details/104459687
08