Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Roman Numbers - Sorting
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 namespace Rextester { class RomanNumbersComparer:IComparer<string>{ public int Compare(string x,string y){return ValueOf(x).CompareTo(ValueOf(y));} public int GetValueOf(string s){return ValueOf(s);} bool IsValidRomanNumber(string s){return true;} // I assume it is valid int ValueOf(char c){ switch(c){ case 'I':return 1; case 'V':return 5; case 'X':return 10; case 'L':return 50; case 'C':return 100; case 'D':return 500; case 'M':return 1000; default:throw new Exception("Invalid Roman Number."); } } int ValueOf(string s){ if(IsValidRomanNumber(s)==false)throw new Exception("Invalid Roman Number"); int acc,q,w,e; acc=q=w=e=0; for(int i=0;i<s.Length;++i){ q=w;w=e;e=ValueOf(s[i]); if(w==0)continue; if(q==0){ if(w<e){acc+=e-w;w=e=0;} else if(w>e){acc+=w;w=0;} }else{ if(q==w&&w==e){acc+=q+w+e;q=w=e=0;} else if(q==w){acc+=q+w;q=w=0;} else{acc+=q;} } } acc+=q+w+e; return acc; } } public class Program { public static void Main(string[] args) { string[] testStrings={ "MCMXCIX", "MXCIX", "III", "II", "III", // repeated "I", "V", "IV", "XCIX", "MXCIX", // repeated "V", // repeated "MI" }; // all strings above must be VALID roman numbers! RomanNumbersComparer rnc=new RomanNumbersComparer(); Array.Sort(testStrings,rnc); // *** ATTENTION HERE! *** foreach(string s in testStrings){ Console.WriteLine("{0}\t\t{1}",s,rnc.GetValueOf(s)); } } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,12 sec, absolute running time: 0,09 sec, cpu time: 0,11 sec, average memory usage: 13 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
I 1 II 2 III 3 III 3 IV 4 V 5 V 5 XCIX 99 MI 1001 MXCIX 1099 MXCIX 1099 MCMXCIX 1999