progs/catastrophic.rb
author Christian Urban <urbanc@in.tum.de>
Thu, 14 Nov 2019 13:17:02 +0000
changeset 687 8865f4f2be59
parent 563 bddf14e026b3
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     1
# A case of catastrophic backtracking in Ruby
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     2
#---------------------------------------------
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     3
# example provided by Daniel Baldwin
558
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
# 
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     6
# regex: (a?){n} a{n}
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     7
# strings: aa...
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     8
#
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     9
# run on the command line with:
558
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
# $>  ruby catastrophic.rb
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    12
#
58
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    13
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
    14
nums = (1..1000)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    16
#iterate through the nums 1-1000
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
nums.each do |i|
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
	start_time = Time.now
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
	string = "a" * i
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    21
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    22
        #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
    23
	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
    24
	re = Regexp.new(re_str)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    26
        re.match(string)
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    27
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    28
        #if re.match(string)
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    29
	#	puts "matched string  a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    30
	#else
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    31
	#	puts "unmatched string a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    32
	#end
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
	
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    34
  puts "#{i} %.5f" % (Time.now - start_time)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
end