progs/catastrophic.rb
author Christian Urban <urbanc@in.tum.de>
Sun, 21 Aug 2016 18:15:53 +0200
changeset 411 1aec0e1fda86
parent 226 progs/re.rb@e3c454e31224
child 420 25bc57b32efa
permissions -rw-r--r--
updated catastrophic examples
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
     1
# provided by Daniel Baldwin
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
     2
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
     3
nums = (1..1000)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
#iterate through the nums 1-100
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
nums.each do |i|
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
	start_time = Time.now
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
	string = "a" * 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
    10
	re_str = "a?" * i + "+" + "a" * i
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
	#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
    12
	
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
    13
	re = Regexp.new(re_str)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    15
        re.match(string)
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    16
	#if re.match(string)
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    17
	#	puts "matched string  a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    18
	#else
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    19
	#	puts "unmatched string a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    20
	#end
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
	
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    22
  puts "#{i} %.5f" % (Time.now - start_time)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
end