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 |
753 | 1 |
#!/usr/bin/env python3 |
745 | 2 |
|
49 | 3 |
import re |
4 |
import sys |
|
5 |
||
613 | 6 |
# case of catastrophic backtracking in Python |
558 | 7 |
# |
613 | 8 |
# regex: (a*)*b |
9 |
# strings: aa...a |
|
558 | 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 | 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 | 16 |
cn = sys.argv[1] |
17 |
||
613 | 18 |
# calling the matching function |
19 |
s = ("a" * int(cn)) |
|
20 |
m = re.match('(a*)*b' , s) |
|
49 | 21 |
|
753 | 22 |
print(s) |
23 |
print(m) |