daily pastebin goal
71%
SHARE
TWEET

Roman->Arabic

Wojtekd Dec 13th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def map
  2.   { "M" => 1000,
  3.     "I" => 1,
  4.     "L" => 50,
  5.     "C" => 100,
  6.     "X" => 10,
  7.     "V" => 50,
  8.     "D" => 500
  9.   }
  10. end
  11.    
  12. def to_arabic(roman)
  13.   value = 0
  14.   for i in 1..roman.length-1
  15.     curr = roman[i]
  16.     prev = roman[i-1]
  17.  
  18.     if(map[prev] < map[curr]) then
  19.       value -= map[prev]
  20.     else
  21.       value += map[prev]
  22.     end
  23.    
  24.   end
  25.   value += map[roman[roman.length-1]]
  26.   return value
  27. end
  28.  
  29. to_arabic("MCMXCIX")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top