summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAndreas Rumpf <andreas@andreas-desktop>2009-11-15 17:46:15 +0100
committerAndreas Rumpf <andreas@andreas-desktop>2009-11-15 17:46:15 +0100
commit281609c358b139d55461af842ce29f39f01b2441 (patch)
treea811f925fc263b0a9c2c021337b3350f096f6953 /tests
parent63e9a88c1f1077b4e7f826324ab772f3c88450b2 (diff)
downloadNim-281609c358b139d55461af842ce29f39f01b2441.tar.gz
fixed typos in documentation
Diffstat (limited to 'tests')
-rwxr-xr-xtests/tromans.nim64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/tromans.nim b/tests/tromans.nim
new file mode 100755
index 000000000..89e3deba8
--- /dev/null
+++ b/tests/tromans.nim
@@ -0,0 +1,64 @@
+import
+  math, strutils
+
+## Convert an integer to a Roman numeral
+# See http://en.wikipedia.org/wiki/Roman_numerals for reference
+
+proc raiseInvalidValue(msg: string) {.noreturn.} =
+  # Yes, we really need a shorthand for this code...
+  var e: ref EInvalidValue
+  new(e)
+  e.msg = msg
+  raise e
+
+# I should use a class, perhaps.
+# --> No. Why introduce additional state into such a simple and nice
+# interface? State is evil. :D
+
+proc ConvertRomanToDecimal(romanVal: string): int =
+  result = 0
+  var prevVal = 0
+  for i in countdown(romanVal.len - 1, 0):
+    var val = 0
+    case romanVal[i]
+    of 'I', 'i': val = 1
+    of 'V', 'v': val = 5
+    of 'X', 'x': val = 10
+    of 'L', 'l': val = 50
+    of 'C', 'c': val = 100
+    of 'D', 'd': val = 500
+    of 'M', 'm': val = 1000
+    else: raiseInvalidValue("Incorrect character in roman numeral! (" & 
+                            $romanVal[i] & ")")
+    if val >= prevVal:
+      inc(result, val)
+    else:
+      dec(result, val)
+    prevVal = val
+
+proc ConvertDecimalToRoman(decValParam: int): string =
+  # Apparently numbers cannot be above 4000
+  # Well, they can be (using overbar or parenthesis notation)
+  # but I see little interest (beside coding challenge) in coding them as
+  # we rarely use huge Roman numeral.
+  const romanComposites = [
+    ("M", 1000), ("CM", 900),
+    ("D", 500), ("CD", 400), ("C", 100),
+    ("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9),
+    ("V", 5), ("IV", 4), ("I", 1)]     
+  if decValParam < 1 or decValParam > 3999:
+    raiseInvalidValue("number not representable")
+  result = ""
+  var decVal = decValParam
+  for key, val in items(romanComposites):
+    while decVal >= val:
+      dec(decVal, val)
+      result.add(key)
+
+randomize()
+for i in 1 .. 10:
+  var rnd = 1 + random(3990)
+  var roman = ConvertDecimalToRoman(rnd)
+  var decimal = ConvertRomanToDecimal(roman)
+  echo("$# => $# => $#" % [ $rnd, roman, $decimal ])
+