Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first conversion done

  • Loading branch information...
commit 8af054955b9b79b254ecfb90b4b6bc0a42fe9f69 1 parent 99135e5
@CodeOfficer CodeOfficer authored
View
84 002/meat-pizza/example_entry.rb
@@ -0,0 +1,84 @@
+# Hint:
+# I = 1
+# V = 5
+# X = 10
+# L = 50
+# C = 100
+# D = 500
+# M = 1000
+
+
+
+class RomanNumeral
+
+
+ def initialize( numeral_string )
+ @numeral_string = numeral_string
+ end
+
+ def +( numeral_string )
+ # "MMX"
+ x = str_to_int(@numeral_string) + str_to_int(numeral_string.to_s)
+ puts x
+ x
+ end
+
+ def -( numeral_string )
+ # "MCMXCVI"
+ x = str_to_int(@numeral_string) - str_to_int(numeral_string.to_s)
+ puts x
+ x
+ end
+
+ def to_s
+ @numeral_string
+ end
+
+ def str_to_int (string)
+ chars = string.split('')
+ last = value_for(chars.shift)
+ value = 0
+
+ chars.each do |c|
+ i = value_for(c)
+ # 1
+ if !last.nil?
+ # 1a
+ if i > last
+ value += (i - last)
+ last = nil
+ # 1b
+ else
+ value += last
+ last = i
+ end
+ # 2
+ else
+ last = i
+ end
+ end
+
+ # 3
+ if last
+ value += last
+ end
+
+ value
+ end
+
+ def value_for(string)
+ case string
+ when 'I' then 1
+ when 'V' then 5
+ when 'X' then 10
+ when 'L' then 50
+ when 'C' then 100
+ when 'D' then 500
+ when 'M' then 1000
+ else
+ raise 'Your Mom!'
+ end
+ end
+
+end
+
View
31 002/meat-pizza/roman_numeral_spec.rb
@@ -0,0 +1,31 @@
+# Pull in some spec settings.
+require File.join( File.dirname(__FILE__), '..', '..', 'spec_helper' )
+
+# Pull in all the Ruby files in this directory.
+Dir.glob( File.join( File.dirname(__FILE__), '*.rb' ) ).each do |file|
+ require file unless file.match /_spec.rb$/
+end
+
+# Given a Roman Numeral string, compute the Roman Numeral answer.
+# Hint:
+# I = 1
+# V = 5
+# X = 10
+# L = 50
+# C = 100
+# D = 500
+# M = 1000
+describe RomanNumeral do
+
+ it "should compute simple arithmatic" do
+
+ @reports << Benchmark.measure do
+ ( RomanNumeral.new( "MCMXCVI" ) + RomanNumeral.new( "XIV" ) ).should eql( "MMX" )
+ ( RomanNumeral.new( "MMX" ) - RomanNumeral.new( "XIV" ) ).should eql( "MCMXCVI" )
+ end
+ end
+
+end
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.