Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Unix Match. Part 2 by Elenka79
def unix_match(filename: str, pattern: str) -> bool:
if pattern == '':
return True
if pattern == filename:
return True
for i in range(len(pattern)):
if pattern[i]=='[':
end = pattern.find(']')
abc = pattern[i+1:end]
if len(abc) == 0: return False
if abc[0] == '!' and not(filename[i] in abc):
return unix_match(filename[i+1:], pattern[end+1:])
elif abc[0] != '!' and filename[i] in abc:
return unix_match(filename[i+1:], pattern[end+1:])
else:
return False
if pattern[i]==filename[i]:
return unix_match(filename[i+1:], pattern[i+1:])
else:
return False
return filename == pattern
if __name__ == '__main__':
print("Example:")
print(unix_match("[!]check.txt","[!]check.txt"))
# These "asserts" are used for self-checking and not for an auto-testing
assert unix_match('somefile.txt', 'somefile.txt') == True
assert unix_match('1name.txt', '[!abc]name.txt') == True
assert unix_match('log1.txt', 'log[!0].txt') == True
assert unix_match('log1.txt', 'log[1234567890].txt') == True
assert unix_match('log1.txt', 'log[!1].txt') == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 30, 2020