Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Concise algorithm solution in Speedy category for Unix Match. Part 1 by Igor_Sekretarev
def unix_match(filename: str, pattern: str) -> bool:
n = len(filename)
dp = [True] + [False]*n
for token in pattern:
if token != '*':
for i in reversed(range(n)):
dp[i+1] = dp[i] and token in (filename[i], '?')
else:
for i in range(n):
dp[i+1] = dp[i] or dp[i+1]
dp[0] = dp[0] and token == '*'
return dp[-1]
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!")
April 25, 2021