Created
November 21, 2017 17:25
-
-
Save jasonrhaas/bff0a8e810fbf6fa1e17dbc18443e560 to your computer and use it in GitHub Desktop.
datadog interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 3.6 | |
import re | |
import logging | |
import unittest | |
from itertools import zip_longest | |
logging.basicConfig(level=logging.DEBUG) | |
def match_pattern(pattern, test): | |
new_pattern = ''.join(['_' * int(x) if x.isdigit() else x for x in pattern]) | |
logging.debug(new_pattern) | |
for x, y in zip_longest(new_pattern, test): | |
logging.debug(f'{x}, {y}') | |
if x == y: | |
continue | |
elif x == '_': | |
continue | |
else: | |
return False | |
return True | |
def test_match_pattern(): | |
success = [('dat3g', 'datadog'), ] | |
failure = [('dat2g', 'datadog'), ] | |
for s in success: | |
assert match_pattern(pattern=s[0], test=s[1]) is True | |
for f in failure: | |
assert match_pattern(pattern=f[0], test=f[1]) is False | |
if __name__ == '__main__': | |
test_match_pattern() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment