Skip to content

Instantly share code, notes, and snippets.

@bschlief
Created September 5, 2012 03:36
Show Gist options
  • Save bschlief/3629948 to your computer and use it in GitHub Desktop.
Save bschlief/3629948 to your computer and use it in GitHub Desktop.
PDX Ruby Hangman Gist Rev 1 vs Rev 2
#!/usr/bin/env ruby
require 'benchmark'
def normalize(s)
s.upcase.gsub(/[^A-Z0-9]/,'')
end
def equal_if_you_squint_rev1(s1, s2)
s1 = normalize(s1)
s1 = normalize(s2)
s1.chars.all? {|c| s2.sub! /.*?#{c}/,''}
end
def equal_if_you_squint_rev2(s1, s2)
s1 = normalize(s1)
s1 = normalize(s2)
!!s2.match(/#{s1.split(//).join('.*')}/)
end
Benchmark.bm do |x|
def test(x, s1,s2)
x.report("#{s1} vs #{s2} rev 1".rjust(40)) { (0..20000).each { equal_if_you_squint_rev1(s1, s2)} }
x.report("#{s1} vs #{s2} rev 2".rjust(40)) { (0..20000).each { equal_if_you_squint_rev2(s1, s2)} }
end
test x, 'co2_saved', 'CO2 saved'
test x, 'kamp_rated','E-Total'
test x, 'eng_type', 'Engine Type'
test x, 'max_temp', 'Max Temperature'
test x, 'max_rpm', 'Max RPM'
test x, 'total_hp', 'Power'
test x, 'power_on', 'Power On'
test x, 'mpg_city', 'Rebate'
test x, 'temp', 'Temperature'
test x, 'fos_c1', 'F.O.S. (C-1)'
test x, 'fos_c2', 'F.O.S.C. #2'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment