Skip to content

Instantly share code, notes, and snippets.

@st0le
Created October 31, 2021 18:54
Show Gist options
  • Save st0le/3d654e32893869f6434819431ad4559c to your computer and use it in GitHub Desktop.
Save st0le/3d654e32893869f6434819431ad4559c to your computer and use it in GitHub Desktop.
def is_match(text, pattern):
assert text != None
assert pattern != None
assert isinstance(text, str)
assert isinstance(pattern, str)
T, P = len(text), len(pattern)
def _is_match(ti, pi):
if pi == P:
return ti == T
# . *
first_char_match = ti != T and (text[ti] == pattern[pi] or pattern[pi] == ".")
if P - pi >= 2:
if pattern[pi + 1] == "*":
return _is_match(ti, pi + 2) or (
first_char_match and _is_match(ti + 1, pi)
)
elif pattern[pi + 1] == "+":
return first_char_match and (
_is_match(ti + 1, pi) or _is_match(ti + 1, pi + 2)
)
return first_char_match and _is_match(ti + 1, pi + 1)
return _is_match(0, 0)
print(is_match("axyzb", "++"))
from itertools import permutations
n = 1543
p = permutations(list(str(n)))
for i in range(2):
print(next(p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment