示例#1
0
        private bool checkSubstract2(string s)
        {
            int length = s.Length;

            if (length <= 2)
            {
                return(true);
            }

            else
            {
                // Check substring of length 3
                for (int i = 2; i < length; i++)
                {
                    RomanSymbol current       = new RomanSymbol(s[i]);
                    RomanSymbol currentMinus1 = new RomanSymbol(s[i - 1]);
                    RomanSymbol currentMinus2 = new RomanSymbol(s[i - 2]);

                    if (current.GetValue() > currentMinus1.GetValue() && current.GetValue() > currentMinus2.GetValue())
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#2
0
        private bool checkSubstractPattern(string s)
        {
            // Rules:
            // "I" can be subtracted from "V" and "X" only
            // "X" can be subtracted from "L" and "C" only.
            // "C" can be subtracted from "D" and "M" only.
            // "V", "L", and "D" can never be subtracted.

            char[] input         = s.ToArray();
            char[] substractable = { 'I', 'X', 'C' };

            for (int i = 0; i < s.Length - 1; i++)
            {
                char        current  = input[i];
                RomanSymbol currentS = new RomanSymbol(current);
                char        next     = input[i + 1];
                RomanSymbol nextS    = new RomanSymbol(next);


                if (substractable.Contains(current))
                {
                    // small values can only be substracted from values <= 10 times of itself
                    if (nextS.GetValue() > currentS.GetValue() * 10)
                    {
                        return(false);
                    }
                }

                else
                {
                    // "V", "L", and "D" can never be subtracted.

                    if (currentS.GetValue() < nextS.GetValue())
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#3
0
        private int ConvertToValue(string s)
        {
            int value = 0;

            if (isValidString(s))
            {
                char[] allSymbols = s.ToUpper().ToArray();

                for (int i = 0; i < allSymbols.Count() - 1; i++)
                {
                    char current      = allSymbols[i];
                    int  currentValue = new RomanSymbol(current).GetValue();
                    char next         = allSymbols[i + 1];
                    int  nextValue    = new RomanSymbol(next).GetValue();

                    if (currentValue >= nextValue)
                    {
                        value += currentValue;
                    }

                    else
                    {
                        value -= currentValue;
                    }
                }

                value += new RomanSymbol(allSymbols.Last()).GetValue();
            }

            else
            {
                value = -1; // invalid string has negative value
            }

            return(value);
        }