Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Unix Match. Part2 solution in Uncategorized category for Unix Match. Part 2 by Elena_Korljukova
def unix_match(filename: str, pattern: str) -> bool:
if pattern == filename:
return True
m = min(pattern.count('['), pattern.count(']'))
a = []
while m != 0:
n1 = pattern.index('[')
n2 = pattern.index(']')+1
a.append(pattern[:n1])
a.append(pattern[n1:n2])
pattern = pattern[n2:]
m -= 1
a.append(pattern)
s = ''
for i in a:
if '[' not in i:
s += i
else:
if '!' not in i:
if filename[len(s)] in i:
s += filename[len(s)]
else:
return False
else:
if filename[len(s)] not in i:
s += filename[len(s)]
else:
return False
return s == filename
if __name__ == '__main__':
print("Example:")
# print(unix_match('somefile.txt', '*'))
print(unix_match("checkio","[c[]heckio"))
# 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!")
June 5, 2020