progs/catastrophic/catastrophic.python
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 10 Sep 2025 15:57:21 +0100
changeset 982 617fc6cfc94a
parent 964 d3e22099963d
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
753
30ea6b01db46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
     1
#!/usr/bin/env python3
745
905b60a029bf 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
6290d4285cee updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     6
# case of catastrophic backtracking in Python
558
c9da2c4586f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
     7
#
613
6290d4285cee updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     8
# regex: (a*)*b
6290d4285cee updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     9
# strings: aa...a
558
c9da2c4586f2 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
#
964
d3e22099963d 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
6290d4285cee updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    18
# calling the matching function
6290d4285cee updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    19
s = ("a" * int(cn))
6290d4285cee 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
30ea6b01db46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    22
print(s)
982
617fc6cfc94a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 964
diff changeset
    23
print(bool(m))