progs/catastrophic2.py
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Sep 2017 12:19:54 +0100
changeset 500 c502933be072
parent 448 96129128d0f1
child 538 17acdd516ccd
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
448
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/usr/bin/env python
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import re
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import sys
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
# case of catastrophic backtracking in Python
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
# regex: (a*)*b
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
# strings: aa...a
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
# call with timing as:
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
#   > time ./catastrophic.py 20
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
# counter n given on the command line
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
cn = sys.argv[1]
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
# calling the matching function
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
m = re.match('(a*)*b' , "a" * int(cn)) 
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
print m.group(0)