| author | Christian Urban <christian.urban@kcl.ac.uk> |
| Sun, 19 Oct 2025 09:51:35 +0200 | |
| changeset 1012 | c01dfa3ff177 |
| parent 985 | c7e944977e39 |
| permissions | -rwxr-xr-x |
| 753 | 1 |
#!/usr/bin/env python3 |
| 448 | 2 |
import re |
3 |
import sys |
|
4 |
||
5 |
# case of catastrophic backtracking in Python |
|
6 |
# |
|
7 |
# regex: (a*)*b |
|
8 |
# strings: aa...a |
|
9 |
# |
|
10 |
# call with timing as: |
|
11 |
# |
|
| 753 | 12 |
# time ./catastrophic2.py 20 |
| 448 | 13 |
|
14 |
# counter n given on the command line |
|
15 |
cn = sys.argv[1] |
|
16 |
||
17 |
# calling the matching function |
|
| 538 | 18 |
s = ("a" * int(cn))
|
| 985 | 19 |
m = re.fullmatch('(a*)*b' , s)
|
| 448 | 20 |
|
| 753 | 21 |
print(s) |
| 985 | 22 |
print(bool(m)) |