progs/catastrophic.py
author Christian Urban <urbanc@in.tum.de>
Wed, 28 Nov 2018 17:13:40 +0000
changeset 219 44161f2c3226
parent 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
#
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
     7
# regex: (a*)*b
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
     8
# strings: aa...a
155
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
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    17
# calling the matching function
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    18
s = ("a" * int(cn))
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    19
m = re.match('(a*)*b' , s) 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    21
print s