progs/catastrophic2.py
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 14:24:46 +0100
changeset 624 8d0af38389bc
parent 538 17acdd516ccd
permissions -rwxr-xr-x
updated to Scala 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
448
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/usr/bin/env python
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import re
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import sys
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
# case of catastrophic backtracking in Python
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
# regex: (a*)*b
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
# strings: aa...a
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
# call with timing as:
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
#
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
#   > time ./catastrophic.py 20
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
# counter n given on the command line
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
cn = sys.argv[1]
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
# calling the matching function
538
17acdd516ccd updated
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
    18
s = ("a" * int(cn))
17acdd516ccd updated
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
    19
m = re.match('(a*)*b' , s) 
448
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
538
17acdd516ccd updated
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
    21
print s