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 Elena_Korljukova
def unix_match(filename: str, pattern: str) -> bool:
if '*' in pattern and '?' in pattern:
return len(pattern)<=len(filename) and all(i in filename for i in pattern.split('*') if i.isalpha())
if '*' in pattern:
return all(i in filename for i in pattern.split('*'))
if '?' in pattern:
return len(pattern)==len(filename) and all(i in filename for i in pattern.split('?'))
return filename == pattern
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