| author | Christian Urban <christian.urban@kcl.ac.uk> |
| Wed, 01 Oct 2025 10:37:20 +0100 | |
| changeset 996 | e37c4d6c08fe |
| parent 983 | ed8d8ba2cb34 |
| 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) |
| 983 | 23 |
print(bool(m)) |