题目:
- matches everything
? matches any single character
Input: Two arguments. Both are strings.
Output: Bool.
Example:
unix_match(‘somefile.txt’, ‘’) == True
unix_match(‘other.exe’, '’) == True
unix_match(‘my.exe’, ‘*.txt’) == False
unix_match(‘log1.txt’, ‘log?.txt’) == True
unix_match(‘log12.txt’, ‘log?.txt’) == False
unix_match(‘log12.txt’, ‘log??.txt’) == True
链接:
https://py.checkio.org/en/mission/unix-match-part-1/
代码:
def unix_match(filename: str, pattern: str) -> bool:
a = filename
b = pattern
r = True
if len(b) > len(a):
r = False
if '?' in b and '*' not in b:
if len(a) != len(b):
r = False
else:
for i in range(0, len(a)):
if a[i] != b[i] and b[i] != '?':
r = False
for i in b:
if i != '*' and i!='?' and i not in a:
r = False
if '.' in a:
x = a.split('.')
if '.' in b:
c = b.split('.')
if c[1] != x[1] and c[1] != '*' and '?' not in c[1]:
r = False
return r