Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Without REGEX - Simple solution in Clear category for Unix Match. Part 1 by riship
def unix_match(filename: str, pattern: str) -> bool:
if pattern.count('?') > len(filename):
return False
while('?' in pattern):
ind = pattern.index('?')
pattern = pattern[0:ind] + pattern[ind+1:]
filename = filename[0:ind] + filename[ind+1:]
l = pattern.split('*')
for i in l:
if i not in filename:
return False
return True
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!")
Aug. 13, 2020