Skip to content

Instantly share code, notes, and snippets.

@rpherrera
Created August 17, 2016 03:06
Show Gist options
  • Save rpherrera/0889a52e277d64c4f4ff184e81c8318c to your computer and use it in GitHub Desktop.
Save rpherrera/0889a52e277d64c4f4ff184e81c8318c to your computer and use it in GitHub Desktop.
asserts the use of star feature from a pseudo-regex parser
def star_regex(regex, string):
if string:
if regex[0] == string[0]:
return star_regex(regex, string[1:])
else:
if regex[len(regex) - 1] == string[0]:
return True
else:
return False
if __name__ == '__main__':
# valid tests
assert(star_regex('a*b', 'ab'))
assert(star_regex('a*b', 'aaaaaaaab'))
assert(star_regex('b*c', 'bc'))
assert(star_regex('b*c', 'bbbbbbbbc'))
# invalid tests - please note the boolean return was inverted
assert(not star_regex('a*b', 'axb'))
assert(not star_regex('a*b', 'aaaaxaaaab'))
assert(not star_regex('b*c', 'byc'))
assert(not star_regex('b*c', 'bbbbybbbbc'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment