progs/catastrophic/catastrophic.python
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 29 Sep 2024 18:46:02 +0100
changeset 965 94f5cce73a4f
parent 753 progs/catastrophic/catastrophic.py@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: 745
diff changeset
     1
#!/usr/bin/env python3
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     2
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import re
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import sys
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     6
# case of catastrophic backtracking in Python
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
     7
#
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     8
# regex: (a*)*b
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     9
# strings: aa...a
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    10
#
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    11
# call with timing as:
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    12
#
965
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    13
#   time ./catastrophic.python 20
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    14
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    15
# counter n given on the command line
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
cn = sys.argv[1]
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    18
# calling the matching function
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    19
s = ("a" * int(cn))
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    20
m = re.match('(a*)*b' , s) 
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    22
print(s)
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    23
print(m)