daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jan 13th, 2012 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun convert(c : Char) : Int {
  2.   when (c) {
  3.     'I' -> return 1
  4.     'V' -> return 5
  5.     'X' -> return 10
  6.     'L' -> return 50
  7.     'C' -> return 100
  8.     'D' -> return 500
  9.     'M' -> return 1000
  10.     else -> return 0
  11.   }
  12. }
  13.  
  14. fun prev(c : Char) : Char {
  15.   when (c) {
  16.     'M' -> return 'C'
  17.     'C' -> return 'X'
  18.     'X' -> return 'I'
  19.     'V' -> return 'I'
  20.     else -> return 'O'
  21.   }
  22. }
  23.  
  24. fun main(args : Array<String>) {
  25.   val number = "MCMLXXXIV"
  26.  
  27.   var result : Int = 0
  28.  
  29.   for (var i in 0 .. number.length - 1) {
  30.     val cur = number.get(i)
  31.     val digit = convert(cur)
  32.     if (i == number.length - 1) {
  33.       result += digit
  34.     } else {
  35.       val next = number.get(i + 1)
  36.       if (prev(next) == cur) {
  37.         result += convert(next) - digit
  38.         i++
  39.       } else {
  40.         result += digit
  41.       }        
  42.     }
  43.   }
  44.  
  45.   System.out?.println(result)
  46. }
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