daily pastebin goal
25%
SHARE
TWEET

12. Integer to Roman

HXXXXJ Feb 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     let romanLUT = [(1000, "M"),
  2.                     (900, "CM"),
  3.                     (500, "D"),
  4.                     (400, "CD"),
  5.                     (100, "C"),
  6.                     (90, "XC"),
  7.                     (50, "L"),
  8.                     (40, "XL"),
  9.                     (10, "X"),
  10.                     (9, "IX"),
  11.                     (5,"V"),
  12.                     (4, "IV"),
  13.                     (1, "I")]
  14.    
  15.     func intToRoman(_ num: Int) -> String
  16.     {
  17.         var remainder = num
  18.         var result = ""
  19.         for (denom, symbol) in romanLUT
  20.         {
  21.             let symCount = remainder / denom
  22.             remainder = remainder % denom
  23.             for _ in 0..<symCount
  24.             {
  25.                 result += symbol
  26.             }
  27.         }
  28.         return result
  29.     }
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