Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Unix Match. Part 1 by vvm70
def unix_match(filename: str, pattern: str) -> bool:
if len(filename) <= pattern.count('?'):
return False
if '*' in pattern:
if set(pattern) in ({'*'}, {'*', '?'}) or pattern == '*.*' and '.' in filename:
return True
if '*.' in pattern:
pattern = pattern.split('.')[-1]
filename = filename.split('.')[-1]
if '.*' in pattern:
pattern = pattern.split('.')[0]
filename = filename.split('.')[0]
names = list(filter(lambda x: '?' not in x, zip(filename, pattern)))
return ''.join(x[0] for x in names) == ''.join(x[1] for x in names)
if __name__ == '__main__':
print("Example:")
print(unix_match('somefile.txt', '*'))
# These "asserts" are used for self-checking and not for an auto-testing
assert unix_match('somefile.txt', '*') == True
assert unix_match('other.exe', '*') == True
assert unix_match('my.exe', '*.txt') == False
assert unix_match('log1.txt', 'log?.txt') == True
assert unix_match('log12.txt', 'log?.txt') == False
assert unix_match('log12.txt', 'log??.txt') == True
print("Coding complete? Click 'Check' to earn cool rewards!")
July 21, 2020
Comments: