Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
native_unix_match_part_1 solution in Clear category for Unix Match. Part 1 by Jon_Red
def unix_match(filename:str,pattern:str)->bool:
a,match,y=False,'',[0]
for x in pattern:
if y[-1]>=len(filename)or y[-1]==-1:return False
if x=='*':a=True
else:
if a:
y.append(filename.find(x,y[-1]))
match+=filename[y[-2]:y[-1]]
a,match,y=False,match+(filename[y[-1]]if x=='?'else x),y+[y[-1]+1]
if a:match+=filename[y[-1]:]
return filename==match
if __name__=='__main__':
# self-checks
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
assert unix_match('apache1.log','*.*')==True
July 26, 2020
Comments: