progs/catastrophic.rb
author Christian Urban <urbanc@in.tum.de>
Wed, 10 May 2017 17:03:21 +0100
changeset 491 d5776c6018f0
parent 474 4bdf0dedd708
child 558 447ed6c7cdad
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
     1
# example provided by Daniel Baldwin
58
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
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
     5
#iterate through the nums 1-1000
57
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
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    10
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
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
	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
    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)
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    16
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    17
        #if re.match(string)
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    18
	#	puts "matched string  a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    19
	#else
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    20
	#	puts "unmatched string a * #{i} with regex #{re}"
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    21
	#end
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
	
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    23
  puts "#{i} %.5f" % (Time.now - start_time)
57
0c96b2c04591 added ruby version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
end