Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

-- added Peter and Topher's solution --

  • Loading branch information...
commit 426c0a18990c7a91b4f7c6ac1b95ab89c39a0365 1 parent 99135e5
CLR authored August 19, 2009

Showing 1 changed file with 47 additions and 2 deletions. Show diff stats Hide diff stats

  1. 49  002/original_spec/example_entry.rb
49  002/original_spec/example_entry.rb
... ...
@@ -1,14 +1,59 @@
1 1
 class RomanNumeral
2 2
 
3 3
   def initialize( numeral_string )
  4
+    @roman_string = numeral_string
4 5
   end
5 6
 
6 7
   def +( numeral_string )
7  
-    "MMX"
  8
+    self.to_I + numeral_string.to_I
8 9
   end
9 10
 
10 11
   def -( numeral_string )
11  
-    "MCMXCVI"
  12
+    self.to_I - numeral_string.to_I
  13
+  end
  14
+
  15
+  def to_I
  16
+    @roman_string.to_a.each do |letter|
  17
+      if @roman_string.shorthand?
  18
+        @longhand - Array( RomanNumeral.scratch_counts( letter ), "I" )
  19
+      else
  20
+        @longhand + Array( RomanNumeral.scratch_counts( letter ), "I" )
  21
+      end
  22
+      @roman_string = [ 1..( @roman_string.length - 1 ) ]
  23
+    end
  24
+  end
  25
+
  26
+  def shorthand?
  27
+    @roman_string[ 1..( @roman_string.length - 1 ) ].detect{ |letter| RomanNumeral.letters.index( @roman_string[0] )
  28
+  end
  29
+
  30
+  def self.scratch_counts( letter )
  31
+    case letter
  32
+    when 'I':
  33
+      1
  34
+    when 'V':
  35
+      5
  36
+    when 'X':
  37
+      10
  38
+    when 'L':
  39
+      50
  40
+    when 'C':
  41
+      100
  42
+    when 'D':
  43
+      500
  44
+    when 'M':
  45
+      1000
  46
+    else
  47
+      raise "ERROR!! Wtf is that -- Greek?"
  48
+    end
  49
+  end
  50
+
  51
+  def self.letters
  52
+    "MDCLXVI"
  53
+  end
  54
+
  55
+  def self.has_shorthand?( letters )
  56
+
12 57
   end
13 58
 end
14 59
 

0 notes on commit 426c0a1

Please sign in to comment.
Something went wrong with that request. Please try again.