progs/catastrophic/catastrophic.rb
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 09 Oct 2022 13:39:34 +0100
changeset 882 5fcad75ade92
parent 753 d94fdbef1a4f
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     1
#!/usr/bin/env ruby
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     2
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     3
# A case of catastrophic backtracking in Ruby
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     4
#---------------------------------------------
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     5
# example provided by Daniel Baldwin
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     6
#
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     7
# 
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     8
# regex: (a?){n} a{n}
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     9
# strings: aa...
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    10
#
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    11
# run on the command line with:
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    12
#
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    13
# ./catastrophic.rb
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    14
#
58
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    15
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    16
nums = (1..30)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    18
#iterate through the nums 1-30
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
nums.each do |i|
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
	start_time = Time.now
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
	string = "a" * i
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    23
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    24
        #create a new regular expression based on current value of i
226
e3c454e31224 updated so that it works with more recent versions of Ruby
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    25
	re_str = "a?" * i + "+" + "a" * i
e3c454e31224 updated so that it works with more recent versions of Ruby
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    26
	re = Regexp.new(re_str)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    28
        re.match(string)
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    29
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    30
        #if re.match(string)
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    31
	#	puts "matched string  a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    32
	#else
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    33
	#	puts "unmatched string a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    34
	#end
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
	
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    36
  puts "#{i} %.5f" % (Time.now - start_time)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
end