progs/catastrophic.py
author Christian Urban <urbanc@in.tum.de>
Fri, 24 Nov 2017 03:10:23 +0000
changeset 155 371acb50643d
child 156 cc6d036401f4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/usr/bin/env python
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import re
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import sys
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
# case of catastrophic backtracking in Python
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
#
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
# regex: (a?){n} a{n}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
# strings: aa...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
#
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
# call with timing as:
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
#
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
#   > time ./catastrophic.py 20
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
# counter n given on the command line
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
cn = sys.argv[1]
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
# constructing the regex
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
r1 = '((a?){%s})' % cn
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
r2 = 'a{%s}' % cn
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
# calling the matching function
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
m = re.match(r1 + r2 , "a" * int(cn)) 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
print m.group(0)