| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Sat, 24 Oct 2020 13:02:18 +0100 | |
| changeset 788 | 0a3ba0769a61 | 
| parent 753 | 30ea6b01db46 | 
| 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  | 
#  | 
| 742 | 13  | 
# time ./catastrophic.py 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)  |