progs/catastrophic/catastrophic2.py
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 25 Sep 2023 15:12:11 +0100
changeset 929 9541e073f2ed
parent 753 d94fdbef1a4f
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     1
#!/usr/bin/env python3
448
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
#
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    12
#    time ./catastrophic2.py 20
448
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
538
17acdd516ccd updated
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
    18
s = ("a" * int(cn))
17acdd516ccd updated
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
    19
m = re.match('(a*)*b' , s) 
448
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    21
print(s)
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    22
print(m)