progs/catastrophic.py
author cu
Thu, 28 Sep 2017 11:04:11 +0100
changeset 510 25580bf89ac0
parent 420 25bc57b32efa
child 558 447ed6c7cdad
permissions -rwxr-xr-x
typos
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/usr/bin/env python
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import re
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import sys
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     5
# case of catastrophic backtracking in Python
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     6
#
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     7
# regex: (a?){n} a{n}
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     8
# strings: aa...
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     9
#
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    10
# call with timing as:
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    11
#
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    12
#   > time ./catastrophic.py 20
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    13
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    14
# counter n given on the command line
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
cn = sys.argv[1]
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    17
# constructing the regex
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
r1 = '((a?){%s})' % cn
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
r2 = 'a{%s}' % cn
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    21
# calling the matching function
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
m = re.match(r1 + r2 , "a" * int(cn)) 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
print m.group(0)