In data sometimes the same product will be named with a roman numeral while other times it will be a digit.
Example Samsung Galaxy SII verses Samsung Galaxy S2
How can the II be converted to the value 2?
I've noticed some really complicated solutions here but this is a really simple problem. I made a solution that avoided the need to hard code the "exceptions" (IV, IX, XL, etc). I used a for loop to look ahead at the next character in the Roman numeral string to see if the number associated with the numeral should be subtracted or added to the total. For simplicity's sake I'm assuming all input is valid.
private static Dictionary<char, int> RomanMap = new Dictionary<char, int>()
{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
public static int RomanToInteger(string roman)
{
int number = 0;
for (int i = 0; i < roman.Length; i++)
{
if (i + 1 < roman.Length && RomanMap[roman[i]] < RomanMap[roman[i + 1]])
{
number -= RomanMap[roman[i]];
}
else
{
number += RomanMap[roman[i]];
}
}
return number;
}
I initially tried using a foreach on the string which I think was a slightly more readable solution but I ended up adding every single number and subtracting it twice later if it turned out to be one of the exceptions, which I didn't like. I'll post it here anyway for posterity.
public static int RomanToInteger(string roman)
{
int number = 0;
char previousChar = roman[0];
foreach(char currentChar in roman)
{
number += RomanMap[currentChar];
if(RomanMap[previousChar] < RomanMap[currentChar])
{
number -= RomanMap[previousChar] * 2;
}
previousChar = currentChar;
}
return number;
}
This is my solution
public int SimplerConverter(string number)
{
number = number.ToUpper();
var result = 0;
foreach (var letter in number)
{
result += ConvertLetterToNumber(letter);
}
if (number.Contains("IV")|| number.Contains("IX"))
result -= 2;
if (number.Contains("XL")|| number.Contains("XC"))
result -= 20;
if (number.Contains("CD")|| number.Contains("CM"))
result -= 200;
return result;
}
private int ConvertLetterToNumber(char letter)
{
switch (letter)
{
case 'M':
{
return 1000;
}
case 'D':
{
return 500;
}
case 'C':
{
return 100;
}
case 'L':
{
return 50;
}
case 'X':
{
return 10;
}
case 'V':
{
return 5;
}
case 'I':
{
return 1;
}
default:
{
throw new ArgumentException("Ivalid charakter");
}
}
}
A more simple and readable C# implementation that:
maps I to 1, V to 5, X to 10, L to 50, C to 100, D to 500, M to 1000.
uses one single foreach loop (foreach used on purpose, with previous value
hold).
adds the mapped number to the total.
subtracts twice the number added before, if I before V or X, X before L or C, C before D or M (not all chars are allowed here!).
returns 0 (not used in Roman numerals) on empty string, wrong letter
or not allowed char used for subtraction.
remark: it's still not totally complete, we didn't check all possible conditions for a valid input string!
Code:
private static Dictionary<char, int> _romanMap = new Dictionary<char, int>
{
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}
};
public static int ConvertRomanToNumber(string text)
{
int totalValue = 0, prevValue = 0;
foreach (var c in text)
{
if (!_romanMap.ContainsKey(c))
return 0;
var crtValue = _romanMap[c];
totalValue += crtValue;
if (prevValue != 0 && prevValue < crtValue)
{
if (prevValue == 1 && (crtValue == 5 || crtValue == 10)
|| prevValue == 10 && (crtValue == 50 || crtValue == 100)
|| prevValue == 100 && (crtValue == 500 || crtValue == 1000))
totalValue -= 2 * prevValue;
else
return 0;
}
prevValue = crtValue;
}
return totalValue;
}
I landed here searching for a small implementation of a Roman Numerals parser but wasn't satisfied by the provided answers in terms of size and elegance. I leave my final, recursive implementation here, to help others searching a small implementation.
Convert Roman Numerals by Recursion
The algorithm is able to handle numerals in irregular subtractive notation (f.e. XIIX).
This implementation may only work with well-formed (strings matching /[mdclxvi]*/i) roman numerals.
The implementation is not optimized for speed.
// returns the value for a roman literal
private static int romanValue(int index)
{
int basefactor = ((index % 2) * 4 + 1); // either 1 or 5...
// ...multiplied with the exponentation of 10, if the literal is `x` or higher
return index > 1 ? (int) (basefactor * System.Math.Pow(10.0, index / 2)) : basefactor;
}
public static int FromRoman(string roman)
{
roman = roman.ToLower();
string literals = "mdclxvi";
int value = 0, index = 0;
foreach (char literal in literals)
{
value = romanValue(literals.Length - literals.IndexOf(literal) - 1);
index = roman.IndexOf(literal);
if (index > -1)
return FromRoman(roman.Substring(index + 1)) + (index > 0 ? value - FromRoman(roman.Substring(0, index)) : value);
}
return 0;
}
Try it using this .Netfiddle: https://dotnetfiddle.net/veaNk3
How does it work?
This algorithm calculates the value of a Roman Numeral by taking the highest value from the Roman Numeral and adding/subtracting recursively the value of the remaining left/right parts of the literal.
ii X iiv # Pick the greatest value in the literal `iixiiv` (symbolized by uppercase)
Then recursively reevaluate and subtract the lefthand-side and add the righthand-side:
(iiv) + x - (ii) # Subtract the lefthand-side, add the righthand-side
(V - (ii)) + x - ((I) + i) # Pick the greatest values, again
(v - ((I) + i)) + x - ((i) + i) # Pick the greatest value of the last numeral compound
Finally the numerals are substituted by their integer values:
(5 - ((1) + 1)) + 10 - ((1) + 1)
(5 - (2)) + 10 - (2)
3 + 10 - 2
= 11
I wrote a simple Roman Numeral Converter just now, but it doesn't do a whole lot of error checking, but it seems to work for everything I could throw at it that is properly formatted.
public class RomanNumber
{
public string Numeral { get; set; }
public int Value { get; set; }
public int Hierarchy { get; set; }
}
public List<RomanNumber> RomanNumbers = new List<RomanNumber>
{
new RomanNumber {Numeral = "M", Value = 1000, Hierarchy = 4},
//{"CM", 900},
new RomanNumber {Numeral = "D", Value = 500, Hierarchy = 4},
//{"CD", 400},
new RomanNumber {Numeral = "C", Value = 100, Hierarchy = 3},
//{"XC", 90},
new RomanNumber {Numeral = "L", Value = 50, Hierarchy = 3},
//{"XL", 40},
new RomanNumber {Numeral = "X", Value = 10, Hierarchy = 2},
//{"IX", 9},
new RomanNumber {Numeral = "V", Value = 5, Hierarchy = 2},
//{"IV", 4},
new RomanNumber {Numeral = "I", Value = 1, Hierarchy = 1}
};
/// <summary>
/// Converts the roman numeral to int, assumption roman numeral is properly formatted.
/// </summary>
/// <param name="romanNumeralString">The roman numeral string.</param>
/// <returns></returns>
private int ConvertRomanNumeralToInt(string romanNumeralString)
{
if (romanNumeralString == null) return int.MinValue;
var total = 0;
for (var i = 0; i < romanNumeralString.Length; i++)
{
// get current value
var current = romanNumeralString[i].ToString();
var curRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == current.ToUpper());
// last number just add the value and exit
if (i + 1 == romanNumeralString.Length)
{
total += curRomanNum.Value;
break;
}
// check for exceptions IV, IX, XL, XC etc
var next = romanNumeralString[i + 1].ToString();
var nextRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == next.ToUpper());
// exception found
if (curRomanNum.Hierarchy == (nextRomanNum.Hierarchy - 1))
{
total += nextRomanNum.Value - curRomanNum.Value;
i++;
}
else
{
total += curRomanNum.Value;
}
}
return total;
}
private static int convertRomanToInt(String romanNumeral)
{
Dictionary<Char, Int32> romanMap = new Dictionary<char, int>
{
{'I', 1 },
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
Int32 result = 0;
for (Int32 index = romanNumeral.Length - 1, last = 0; index >= 0; index--)
{
Int32 current = romanMap[romanNumeral[index]];
result += (current < last ? -current : current);
last = current;
}
return result;
}
With Humanizer library you can change Roman numerals to numbers using the FromRoman extension
"IV".FromRoman() => 4
And reverse operation using the ToRoman extension.
4.ToRoman() => "IV"
Borrowed a lot from System.Linq on this one. String implements IEnumerable<char>, so I figured that was appropriate since we are treating it as an enumerable object anyways. Tested it against a bunch of random numbers, including 1, 3, 4, 8, 83, 99, 404, 555, 846, 927, 1999, 2420.
public static IDictionary<char, int> CharValues
{
get
{
return new Dictionary<char, int>
{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
}
}
public static int RomanNumeralToInteger(IEnumerable<char> romanNumerals)
{
int retVal = 0;
//go backwards
for (int i = romanNumerals.Count() - 1; i >= 0; i--)
{
//get current character
char c = romanNumerals.ElementAt(i);
//error checking
if (!CharValues.ContainsKey(c)) throw new InvalidRomanNumeralCharacterException(c);
//determine if we are adding or subtracting
bool op = romanNumerals.Skip(i).Any(rn => CharValues[rn] > CharValues[c]);
//then do so
retVal = op ? retVal - CharValues[c] : retVal + CharValues[c];
}
return retVal;
}
Solution with fulfilling the "subtractive notation" semantics checks
None of the current solutions completely fulfills the entire set of rules
for the "subtractive notation". "IIII" -> is not possible. Each of the solutions results a 4. Also the strings: "CCCC", "VV", "IC", "IM" are invalid.
A good online-converter to check the semantics is https://www.romannumerals.org/converter
So, if you really want doing a completely semantics-check, it's much more complex.
My approach was to first write the unit tests with the semantic checks. Then to write the code. Then to reduce the loops with some linq expressions.
Maybe there is a smarter solution, but I think the following code fullfills the rules to convert a roman numerals string.
After the code section, there is a section with my unit tests.
public class RomanNumerals
{
private List<Tuple<char, ushort, char?[]>> _validNumerals = new List<Tuple<char, ushort, char?[]>>()
{
new Tuple<char, ushort, char?[]>('I', 1, new char? [] {'V', 'X'}),
new Tuple<char, ushort, char?[]>('V', 5, null),
new Tuple<char, ushort, char?[]>('X', 10, new char?[] {'L', 'C'}),
new Tuple<char, ushort, char?[]>('L', 50, null),
new Tuple<char, ushort, char?[]>('C', 100, new char? [] {'D', 'M'}),
new Tuple<char, ushort, char?[]>('D', 500, null),
new Tuple<char, ushort, char?[]>('M', 1000, new char? [] {null, null})
};
public int TranslateRomanNumeral(string input)
{
var inputList = input?.ToUpper().ToList();
if (inputList == null || inputList.Any(x => _validNumerals.Select(t => t.Item1).Contains(x) == false))
{
throw new ArgumentException();
}
char? valForSubtraction = null;
int result = 0;
bool noAdding = false;
int equalSum = 0;
for (int i = 0; i < inputList.Count; i++)
{
var currentNumeral = _validNumerals.FirstOrDefault(s => s.Item1 == inputList[i]);
var nextNumeral = i < inputList.Count - 1 ? _validNumerals.FirstOrDefault(s => s.Item1 == inputList[i + 1]) : null;
bool currentIsDecimalPower = currentNumeral?.Item3?.Any() ?? false;
if (nextNumeral != null)
{
// Syntax and Semantics checks
if ((currentNumeral.Item2 < nextNumeral.Item2) && (currentIsDecimalPower == false || currentNumeral.Item3.Any(s => s == nextNumeral.Item1) == false) ||
(currentNumeral.Item2 == nextNumeral.Item2) && (currentIsDecimalPower == false || nextNumeral.Item1 == valForSubtraction) ||
(currentIsDecimalPower && result > 0 && ((nextNumeral.Item2 -currentNumeral.Item2) > result )) ||
(currentNumeral.Item2 > nextNumeral.Item2) && (nextNumeral.Item1 == valForSubtraction)
)
{
throw new ArgumentException();
}
if (currentNumeral.Item2 == nextNumeral.Item2)
{
equalSum += equalSum == 0 ? currentNumeral.Item2 + nextNumeral.Item2 : nextNumeral.Item2;
int? smallest = null;
var list = _validNumerals.Where(p => _validNumerals.FirstOrDefault(s => s.Item1 == currentNumeral.Item1).Item3.Any(s2 => s2 != null && s2 == p.Item1)).ToList();
if (list.Any())
{
smallest = list.Select(s3 => s3.Item2).ToList().Min();
}
// Another Semantics check
if (currentNumeral.Item3 != null && equalSum >= (smallest - currentNumeral.Item2))
{
throw new ArgumentException();
}
result += noAdding ? 0 : currentNumeral.Item2 + nextNumeral.Item2;
noAdding = !noAdding;
valForSubtraction = null;
}
else
if (currentNumeral.Item2 < nextNumeral.Item2)
{
equalSum = 0;
result += nextNumeral.Item2 - currentNumeral.Item2;
valForSubtraction = currentNumeral.Item1;
noAdding = true;
}
else
if (currentNumeral.Item2 > nextNumeral.Item2)
{
equalSum = 0;
result += noAdding ? 0 : currentNumeral.Item2;
noAdding = false;
valForSubtraction = null;
}
}
else
{
result += noAdding ? 0 : currentNumeral.Item2;
}
}
return result;
}
}
Here are the UNIT tests
[TestFixture]
public class RomanNumeralsTests
{
[Test]
public void TranslateRomanNumeral_WhenArgumentIsNull_RaiseArgumentNullException()
{
var romanNumerals = new RomanNumerals();
Assert.Throws<ArgumentException>(() => romanNumerals.TranslateRomanNumeral(null));
}
[TestCase("A")]
[TestCase("-")]
[TestCase("BXA")]
[TestCase("MMXK")]
public void TranslateRomanNumeral_WhenInvalidNumeralSyntax_RaiseException(string input)
{
var romanNumerals = new RomanNumerals();
Assert.Throws<ArgumentException>(() => romanNumerals.TranslateRomanNumeral(input));
}
[TestCase("IIII")]
[TestCase("CCCC")]
[TestCase("VV")]
[TestCase("IC")]
[TestCase("IM")]
[TestCase("XM")]
[TestCase("IL")]
[TestCase("MCDXCXI")]
[TestCase("MCDDXC")]
public void TranslateRomanNumeral_WhenInvalidNumeralSemantics_RaiseException(string input)
{
var romanNumerals = new RomanNumerals();
Assert.Throws<ArgumentException>(() => romanNumerals.TranslateRomanNumeral(input));
}
[TestCase("I", 1)]
[TestCase("II", 2)]
[TestCase("III", 3)]
[TestCase("IV", 4)]
[TestCase("XLII", 42)]
[TestCase("MMXIII", 2013)]
[TestCase("MXI", 1011)]
[TestCase("MCDXCIX", 1499)]
[TestCase("MMXXII", 2022)]
[TestCase("V", 5)]
[TestCase("VI", 6)]
[TestCase("CX", 110)]
[TestCase("CCCLXXV", 375)]
[TestCase("MD", 1500)]
[TestCase("MDLXXV", 1575)]
[TestCase("MDCL", 1650)]
[TestCase("MDCCXXV", 1725)]
[TestCase("MDCCC", 1800)]
[TestCase("MDCCCLXXV", 1875)]
[TestCase("MCML", 1950)]
[TestCase("MMXXV", 2025)]
[TestCase("MMC", 2100)]
[TestCase("MMCLXXV", 2175)]
[TestCase("MMCCL", 2250)]
[TestCase("MMCCCXXV", 2325)]
[TestCase("MMCD", 2400)]
[TestCase("MMCDLXXV", 2475)]
[TestCase("MMDL", 2550)]
[TestCase("MMMMMMMM", 8000)]
[TestCase("MMMMMMMMIV", 8004)]
public void TranslateRomanNumeral_WhenValidNumeral_Translate(string input, int output)
{
var romanNumerals = new RomanNumerals();
var result = romanNumerals.TranslateRomanNumeral(input);
Assert.That(result.Equals(output));
}
}
private static Dictionary<char, int> RomanNumberMap = new Dictionary<char, int> {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
private const string RomanNumberValidationRegEx = "^(?=[MDCLXVI])M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$";
private static int ConvertToRomanNumberToInteger(string romanNumber)
{
if (!Regex.IsMatch(romanNumber, RomanNumberValidationRegEx))
{
throw new ArgumentOutOfRangeException(romanNumber);
}
int result = 0;
for (int i = 0; i < romanNumber.Length; i++)
{
int currentVal = RomanNumberMap[romanNumber[i]];
if (romanNumber.Length > i + 1)
{
int nextVal = RomanNumberMap[romanNumber[i + 1]];
if (nextVal > currentVal)
{
result = result + (nextVal - currentVal);
i++;
continue;
}
}
result = result + currentVal;
}
return result;
}
I will suggest a simplest method for this by using array in .net : comments are given in C# section for explanation
VB.net
Public Class Form1
Dim indx() As Integer = {1, 2, 3, 4, 5, 10, 50, 100, 500, 1000}
Dim row() As String = {"I", "II", "III", "IV", "V", "X", "L", "C", "D", "M"}
Dim limit As Integer = 9
Dim output As String = ""
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
output = ""
num = CInt(txt1.Text)
While num > 0
num = find(num)
End While
txt2.Text = output
End Sub
Public Function find(ByVal Num As Integer) As Integer
Dim i As Integer = 0
While indx(i) <= Num
i += 1
End While
If i <> 0 Then
limit = i - 1
Else
limit = 0
End If
output = output & row(limit)
Num = Num - indx(limit)
Return Num
End Function
End Class
C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
int[] indx = {
1,
2,
3,
4,
5,
10,
50,
100,
500,
1000
// initialize array of integers
};
string[] row = {
"I",
"II",
"III",
"IV",
"V",
"X",
"L",
"C",
"D",
"M"
//Carasponding roman letters in for the numbers in the array
};
// integer to indicate the position index for link two arrays
int limit = 9;
//string to store output
string output = "";
private void Button1_Click(System.Object sender, System.EventArgs e)
{
int num = 0;
// stores the input
output = "";
// clear output before processing
num = Convert.ToInt32(txt1.Text);
// get integer value from the textbox
//Loop until the value became 0
while (num > 0) {
num = find(num);
//call function for processing
}
txt2.Text = output;
// display the output in text2
}
public int find(int Num)
{
int i = 0;
// loop variable initialized with 0
//Loop until the indx(i).value greater than or equal to num
while (indx(i) <= Num) {
i += 1;
}
// detemine the value of limit depends on the itetration
if (i != 0) {
limit = i - 1;
} else {
limit = 0;
}
output = output + row(limit);
//row(limit) is appended with the output
Num = Num - indx(limit);
// calculate next num value
return Num;
//return num value for next itetration
}
}
I refer from this blog. You could just reverse the roman numeral , then all the thing would be more easier compare to make the comparison.
public static int pairConversion(int dec, int lastNum, int lastDec)
{
if (lastNum > dec)
return lastDec - dec;
else return lastDec + dec;
}
public static int ConvertRomanNumtoInt(string strRomanValue)
{
var dec = 0;
var lastNum = 0;
foreach (var c in strRomanValue.Reverse())
{
switch (c)
{
case 'I':
dec = pairConversion(1, lastNum, dec);
lastNum = 1;
break;
case 'V':
dec=pairConversion(5,lastNum, dec);
lastNum = 5;
break;
case 'X':
dec = pairConversion(10, lastNum, dec);
lastNum = 10;
break;
case 'L':
dec = pairConversion(50, lastNum, dec);
lastNum = 50;
break;
case 'C':
dec = pairConversion(100, lastNum, dec);
lastNum = 100;
break;
case 'D':
dec = pairConversion(500, lastNum, dec);
lastNum = 500;
break;
case 'M':
dec = pairConversion(1000, lastNum, dec);
lastNum = 1000;
break;
}
}
return dec;
}
This one uses a stack:
public int RomanToInt(string s)
{
var dict = new Dictionary<char, int>();
dict['I'] = 1;
dict['V'] = 5;
dict['X'] = 10;
dict['L'] = 50;
dict['C'] = 100;
dict['D'] = 500;
dict['M'] = 1000;
Stack<char> st = new Stack<char>();
foreach (char ch in s.ToCharArray())
st.Push(ch);
int result = 0;
while (st.Count > 0)
{
var c1=st.Pop();
var ch1 = dict[c1];
if (st.Count > 0)
{
var c2 = st.Peek();
var ch2 = dict[c2];
if (ch2 < ch1)
{
result += (ch1 - ch2);
st.Pop();
}
else
{
result += ch1;
}
}
else
{
result += ch1;
}
}
return result;
}
I wrote this just using arrays.
I omit the testing code here, but it looks it works properly.
public static class RomanNumber {
static string[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
static string[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
static string[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
static string[] thousands = { "", "M", "MM", "MMM" };
static public bool IsRomanNumber(string source) {
try {
return RomanNumberToInt(source) > 0;
}
catch {
return false;
}
}
/// <summary>
/// Parses a string containing a roman number.
/// </summary>
/// <param name="source">source string</param>
/// <returns>The integer value of the parsed roman numeral</returns>
/// <remarks>
/// Throws an exception on invalid source.
/// Throws an exception if source is not a valid roman number.
/// Supports roman numbers from "I" to "MMMCMXCIX" ( 1 to 3999 )
/// NOTE : "IMMM" is not valid</remarks>
public static int RomanNumberToInt(string source) {
if (String.IsNullOrWhiteSpace(source)) {
throw new ArgumentNullException();
}
int total = 0;
string buffer = source;
// parse the last four characters in the string
// each time we check the buffer against a number array,
// starting from units up to thousands
// we quit as soon as there are no remaing characters to parse
total += RipOff(buffer, units, out buffer);
if (buffer != null) {
total += (RipOff(buffer, tens, out buffer)) * 10;
}
if (buffer != null) {
total += (RipOff(buffer, hundreds, out buffer)) * 100;
}
if (buffer != null) {
total += (RipOff(buffer, thousands, out buffer)) * 1000;
}
// after parsing for thousands, if there is any character left, this is not a valid roman number
if (buffer != null) {
throw new ArgumentException(String.Format("{0} is not a valid roman number", buffer));
}
return total;
}
/// <summary>
/// Given a string, takes the four characters on the right,
/// search an element in the numbers array and returns the remaing characters.
/// </summary>
/// <param name="source">source string to parse</param>
/// <param name="numbers">array of roman numerals</param>
/// <param name="left">remaining characters on the left</param>
/// <returns>If it finds a roman numeral returns its integer value; otherwise returns zero</returns>
public static int RipOff(string source, string[] numbers, out string left) {
int result = 0;
string buffer = null;
// we take the last four characters : this is the length of the longest numeral in our arrays
// ("VIII", "LXXX", "DCCC")
// or all if source length is 4 or less
if (source.Length > 4) {
buffer = source.Substring(source.Length - 4);
left = source.Substring(0, source.Length - 4);
}
else {
buffer = source;
left = null;
}
// see if buffer exists in the numbers array
// if it does not, skip the first character and try again
// until buffer contains only one character
// append the skipped character to the left arguments
while (!numbers.Contains(buffer)) {
if (buffer.Length == 1) {
left = source; // failed
break;
}
else {
left += buffer.Substring(0, 1);
buffer = buffer.Substring(1);
}
}
if (buffer.Length > 0) {
if (numbers.Contains(buffer)) {
result = Array.IndexOf(numbers, buffer);
}
}
return result;
}
}
}
EDIT
Forget about it !
Just look at BrunoLM solution here.
It's simple and elegant.
The only caveat is that it does not check the source.
This is my solution:
/// <summary>
/// Converts a Roman number string into a Arabic number
/// </summary>
/// <param name="romanNumber">the Roman number string</param>
/// <returns>the Arabic number (0 if the given string is not convertible to a Roman number)</returns>
public static int ToArabicNumber(string romanNumber)
{
string[] replaceRom = { "CM", "CD", "XC", "XL", "IX", "IV" };
string[] replaceNum = { "DCCCC", "CCCC", "LXXXX", "XXXX", "VIIII", "IIII" };
string[] roman = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int[] arabic = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
return Enumerable.Range(0, replaceRom.Length)
.Aggregate
(
romanNumber,
(agg, cur) => agg.Replace(replaceRom[cur], replaceNum[cur]),
agg => agg.ToArray()
)
.Aggregate
(
0,
(agg, cur) =>
{
int idx = Array.IndexOf(roman, cur.ToString());
return idx < 0 ? 0 : agg + arabic[idx];
},
agg => agg
);
}
/// <summary>
/// Converts a Arabic number into a Roman number string
/// </summary>
/// <param name="arabicNumber">the Arabic number</param>
/// <returns>the Roman number string</returns>
public static string ToRomanNumber(int arabicNumber)
{
string[] roman = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int[] arabic = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
return Enumerable.Range(0, arabic.Length)
.Aggregate
(
Tuple.Create(arabicNumber, string.Empty),
(agg, cur) =>
{
int remainder = agg.Item1 % arabic[cur];
string concat = agg.Item2 + string.Concat(Enumerable.Range(0, agg.Item1 / arabic[cur]).Select(num => roman[cur]));
return Tuple.Create(remainder, concat);
},
agg => agg.Item2
);
}
Here's the Explanation how the methods work:
ToArabicNumber
First aggregation step is to Replace the Roman Number special cases (e.g.: IV -> IIII). Second Aggregate step simply sums up the equivalent Arabic number of the Roman letter (e.g. V -> 5)
ToRomanNumber:
I start the aggregation with the given Arabic number. For each step the number will be divided by the equivalent number of the Roman letter. The remainder of this division is then the input for the next step. The division Result will be translated to the Equivalent Roman Number character which will be appended to the result string.
private static HashMap<Character, Integer> romanMap = new HashMap<>() {{
put('I', 1); put('V', 5); put('X', 10); put('L', 50);
put('C', 100); put('D', 500); put('M', 1000);
}};
private static int convertRomanToInt(String romanNumeral) {
int total = 0;
romanNumeral = romanNumeral.toUpperCase();
//add every Roman numeral
for(int i = 0; i < romanNumeral.length(); i++) {
total += romanMap.get(romanNumeral.charAt(i));
}
//remove the Roman numerals that are followed
//directly by a larger Roman numeral
for(int i = 0; i < romanNumeral.length()-1; i++) {
if(romanMap.get(romanNumeral.charAt(i))
< romanMap.get(romanNumeral.charAt(i+1))) {
total -= 2* romanMap.get(romanNumeral.charAt(i));
}
}
return total;
}
//note that the topmost solution does not solve this Roman numeral
//but mine does
//also note that this solution is a preference of simplicity over complexity
public static void main(String[] args) {
String rn = "CcLXxiV"; //274
System.out.println("Convert " + rn + " to " + convertRomanToInt(rn));
}
/*
this uses the string object Replace() & Split() methods
*/
int ToNumber(string roman){
/*
the 0 padding after the comma delimiter allows adding up the extra index produced by Split, which is not numerical
*/
string s1=roman.Replace("CM","900,0");
s1=s1.Replace("M","1000,0");
s1=s1.Replace("CD","400,0");
s1=s1.Replace("D","500,0");
s1=s1.Replace("XC","90,0");
s1=s1.Replace("C","100,0");
s1=s1.Replace("XL","40,0");
s1=s1.Replace("L","50,0");
s1=s1.Replace("IX","9,0");
s1=s1.Replace("X","10,0");
s1=s1.Replace("IV","4,0");
s1=s1.Replace("V","5,0");
s1=s1.Replace("I","1,0");
string[] spl=s1.Split(",");
int rlt=0;
for(int i=0;i<spl.Count();i++)
{
rlt+= Convert.ToInt32(spl[i].ToString());
}
return rlt;
}
Here is my O(n) solution in JavaScript
const NUMERAL = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
var romanToInt = function(s) {
if (s.length === 1) return +NUMERAL[s];
let number = 0;
for (let i = 0; i < s.length; i++) {
let num = +NUMERAL[s[i]];
let prev = +NUMERAL[s[i-1]];
if (prev < num) number += num - (2 * prev);
else number += num;
}
return number;
};
FWIW, here is a "try parse" version of David DeMar's answer:
private static readonly Dictionary<char, int> _romanMap = new Dictionary<char, int>() { { 'I', 1 }, { 'V', 5 }, { 'X', 10 }, { 'L', 50 }, { 'C', 100 }, { 'D', 500 }, { 'M', 1000 } };
public static bool TryParseRoman(string text, out int value)
{
value = 0;
if (string.IsNullOrEmpty(text))
return false;
var number = 0;
for (var i = 0; i < text.Length; i++)
{
if (!_romanMap.TryGetValue(text[i], out var num))
return false;
if ((i + 1) < text.Length)
{
if (!_romanMap.TryGetValue(text[i + 1], out var num2))
return false;
if (num < num2)
{
number -= num;
continue;
}
}
number += num;
}
value = number;
return true;
}
public class Solution {
public int RomanToInt(string s) {
var dict = new Dictionary<char,int>{
{'I',1},
{'V',5},
{'X',10},
{'L',50},
{'C',100},
{'M',1000},
{'D',500},
};
var result = 0; //What am I gonna return it from method ?
for(int i=0; i<s.Length; i++)
{
if(i+1<s.Length && dict[s[i]] < dict[s[i+1]])
{
result -=dict[s[i]];
}
else
{
result +=dict[s[i]];
}
}
return result;
}
}
public int RomanToInt(string s)
{
char[] romans = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
int[] nums = { 1, 5, 10, 50, 100, 500, 1000 };
int result = 0;
foreach (char c in s)
{
for (int i = 0; i < romans.Length; i++)
{
if (romans[i] == c)
{
result += nums[i];
}
}
}
if (s.Contains("IV") || s.Contains("IX"))
result -= 2;
if (s.Contains("XL") || s.Contains("XC"))
result -= 20;
if (s.Contains("CD") || s.Contains("CM"))
result -= 200;
return result;
}
public static int ConvertRomanNumtoInt(string strRomanValue)
{
Dictionary RomanNumbers = new Dictionary
{
{"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}
};
int retVal = 0;
foreach (KeyValuePair pair in RomanNumbers)
{
while (strRomanValue.IndexOf(pair.Key.ToString()) == 0)
{
retVal += int.Parse(pair.Value.ToString());
strRomanValue = strRomanValue.Substring(pair.Key.ToString().Length);
}
}
return retVal;
}
Related
I am trying to build a calculator that will tell me what "steps" are needed to stack up to a given value. Using only that "steps" available in the array.
Example:
decimal[] blocks {.05, .100, .150, .200, .250}
goal = .550m
result = .100, .200, .250
I have tried using nested if statements and array find/last with not much luck.
I can get a match if the goal is an exact match, or will match with two of them stacked. I can't get it to work for the max(.750).
This is what I have so far:
code:
string result = "nope";
decimal goal = 3.264m;
decimal[] DAStep = new decimal[10];
decimal temp = Array.Find(GaugeBlockArray, element => element.Equals(goal));
if (temp != 0m)
{
DAStep[0] = Array.Find(GaugeBlockArray, element => element.Equals(temp));
result = DAStep[0].ToString();
}
else
{
DAStep[0] = GaugeBlockArray.Last(element => element <= goal); ;
decimal remaining;
remaining = goal - DAStep[0];
while (remaining != 0m)
{
DAStep[1] = GaugeBlockArray.Last(element => element <= remaining);
if (DAStep[1] != remaining)
{
DAStep[2] = GaugeBlockArray.Last(element => element <= (DAStep[1] - .0001m));
if (DAStep[2] == 0) { DAStep[1] = DAStep[2]; }
}
}
}
GaugeBlockArray contains an array of 72 different elements from .05 to 4.0. And, I can only use each block once.
edit:
I guess more detail on the array contents may help getting to a solution.
GaugeBlockArray:
.05
.100
.1001
.1002
.1003
.1004
.1005
.1006
.1007
.1008
.1009
.110
.111
.112
.113
.114
.115
.116
.117
.118
.119
.120
.121
.122
.123
.124
.125
.126
.127
.128
.129
.130
.131
.132
.133
.134
.135
.136
.137
.138
.139
.140
.141
.142
.143
.144
.145
.146
.147
.148
.149
.150
.200
.250
.300
.350
.400
.450
.500
.550
.600
.650
.700
.750
.800
.850
.900
.950
1.000
2.000
3.000
4.000
Many thanks to #GeorgPatscheider for getting me pointed in the right direction!
This is my final working result:
public static void CountSum(decimal[] DNumbers, decimal Dsum)
{
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(MetTracker.GaugeCalc))
{
(window as MetTracker.GaugeCalc).CalculateBtn.Content = "working...";
}
}
DNumbers = Array.ConvertAll(DNumbers, element => 10000m * element);
string TempString = GetSettingsStrings("GBCMaxStep"); // only used to initialize max step value
Dsum = Dsum * 10000m;
Int32 Isum = Convert.ToInt32(Dsum);
Int32[] INumbers = Array.ConvertAll(DNumbers, element => (Int32)element);
// int result = 0;
GetmNumberOfSubsets(INumbers, Isum);
success = false;
return;
}
private static void GetmNumberOfSubsets(Int32[] numbers, Int32 Isum)
{
set = numbers;
sum = Isum;
FindSubsetSum();
}
//-------------------------------------------------------------
static Int32[] set;
static Int32[] subSetIndexes;
static Int32 sum;
static Int32 numberOfSubsetSums;
static bool success = false;
static List<Int32> ResultSet = new List<Int32>();
static List<string> results = new List<string>();//------------------------------------------------------------
/*
Method: FindSubsetSum()
*/
private static void FindSubsetSum()
{
numberOfSubsetSums = 0;
Int32 numberOfElements = set.Length;
FindPowerSet(numberOfElements);
}
//-------------------------------------------------------------
/*
Method: FindPowerSet(int n, int k)
*/
private static void FindPowerSet(Int32 n)
{
// Super set - all sets with size: 0, 1, ..., n - 1
for (Int32 k = 0; k <= n - 1; k++)
{
subSetIndexes = new Int32[k];
CombinationsNoRepetition(k, 0, n - 1);
if(subSetIndexes.Length >= GBC_MaxStepSetting) {
break; }
}
if (numberOfSubsetSums == 0)
{
MessageBox.Show("No subsets with wanted sum exist.");
}
}
//-------------------------------------------------------------
/*
Method: CombinationsNoRepetition(int k, int iBegin, int iEnd);
*/
private static void CombinationsNoRepetition(Int32 k, Int32 iBegin, Int32 iEnd)
{
if (k == 0)
{
PrintSubSet();
return;
}
if (success == false)
{
for (Int32 i = iBegin; i <= iEnd; i++)
{
subSetIndexes[k - 1] = i;
++iBegin;
CombinationsNoRepetition(k - 1, iBegin, iEnd);
if (success == true)
break;
}
}
return;
}
private static void PrintSubSet()
{
Int32 currentSubsetSum = 0;
// accumulate sum of current subset
for (Int32 i = 0; i < subSetIndexes.Length; i++)
{
currentSubsetSum += set[subSetIndexes[i]];
if(currentSubsetSum > sum) { break; }
}
if(currentSubsetSum > sum) { return; }
// if wanted sum: print current subset elements
if (currentSubsetSum == sum)
{
++numberOfSubsetSums;
// results.Add("(");
for (Int32 i = 0; i < subSetIndexes.Length; i++)
{
results.Add((set[subSetIndexes[i]]).ToString());
ResultSet.Add(set[subSetIndexes[i]]);
if (i < subSetIndexes.Length - 1)
{
// results.Add(" ,");
}
}
// results.Add(")");
Int32[] ResultSetArr = ResultSet.ToArray();
decimal[] ResultSetArrD = Array.ConvertAll(ResultSetArr, element => (decimal)element);
ResultSetArrD = Array.ConvertAll(ResultSetArrD, element => element / 10000m);
// var message = string.Join(Environment.NewLine, ResultSetArrD);
// message = string.Format("{0:0.0000}", message);
int l = ResultSetArrD.Length;
string[] ResultString = new string[l];
foreach(int i in ResultSetArrD)
{ResultString = Array.ConvertAll(ResultSetArrD, element => element.ToString("0.0000"));}
var message = string.Join(Environment.NewLine, ResultString);
decimal ResultSum = ResultSetArrD.Sum();
MessageBox.Show(message + "\n= " + ResultSum.ToString("0.0000"), "Result");
Array.Clear(ResultSetArrD, 0, ResultSetArrD.Length);
Array.Clear(ResultSetArr, 0, ResultSetArr.Length);
ResultSet.Clear();
message = null;
success = true;
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(MetTracker.GaugeCalc))
{
(window as MetTracker.GaugeCalc).CalculateBtn.Content = "Calculate";
}
}
return;
}
if (success == true)
return;
}
I added some limiting to reduce the amount of time before it reports a failure to find a combo. I also convert the array to a double to get around the headache the decimals were causing me. Works great!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Not sure why question is being marked as offtopic, where as so called desired behaviour is included within the question post!
I am trying to write this program that takes two inputs:
• a set of include intervals
• and a set of exclude intervals
The sets of intervals can be given in any order, and they may be empty or overlapping. The program should output the result of taking all the includes and “remove” the excludes. The output should be given as non-overlapping intervals in a sorted order.
Intervals will contain Integers only
Example :
Includes: 50-600, 10-100
Excludes: (empty)
Output: 10-600
Includes: 10-100, 200-300, 400-600
Excludes: 95-205, 410-420
Output: 10-94, 206-300, 400-409, 421-600
I tried to populate two Enumerable Range from include and excludes (after splitting,parsing ), but didn't find any efficient way of implementing this afterwards.
string[] _break = _string.Split(',');
string[] _breakB = _stringB.Split(',');
string[] res = new string[_break.Length + 1];
string[] _items, _itemsB;
List < int > _back = new List < int > ();
int count = 0;
foreach(var _item in _break) {
_items = _item.Split('-');
var a = Enumerable.Range(int.Parse(_items[0]), (int.Parse(_items[1]) - int.Parse(_items[0]) + 1)).ToList();
foreach(var _itemB in _breakB) {
_itemsB = _itemB.Split('-');
var b = Enumerable.Range(int.Parse((_itemsB[0])), (int.Parse(_itemsB[1]) - int.Parse((_itemsB[0])) + 1)).ToList();
var c = a.Except < int > (b).ToList();
/// different things tried here, but they are not good
res[count] = c.Min().ToString() + "-" + c.Max().ToString();
count++;
}
}
return res;
Any input will be of great help
You can use the Built-in SortedSet<T> collection to do most of the work for you like this:
The SortedSet<T> collection implements the useful UnionWith and ExceptWith methods which at least makes the code quite easy to follow:
private void button1_Click(object sender, EventArgs e)
{
string[] includeRanges = _string.Text.Replace(" ", "").Split(',');
string[] excludeRanges = _stringB.Text.Replace(" ", "").Split(',');
string[] includeRange, excludeRange;
SortedSet<int> includeSet = new SortedSet<int>();
SortedSet<int> excludeSet = new SortedSet<int>();
// Create a UNION of all the include ranges
foreach (string item in includeRanges)
{
includeRange = item.Split('-');
includeSet.UnionWith(Enumerable.Range(int.Parse(includeRange[0]), (int.Parse(includeRange[1]) - int.Parse(includeRange[0]) + 1)).ToList());
}
// Create a UNION of all the exclude ranges
foreach (string item in excludeRanges)
{
excludeRange = item.Split('-');
excludeSet.UnionWith(Enumerable.Range(int.Parse(excludeRange[0]), (int.Parse(excludeRange[1]) - int.Parse(excludeRange[0]) + 1)).ToList());
}
// Exclude the excludeSet from the includeSet
includeSet.ExceptWith(excludeSet);
//Format the output using a stringbuilder
StringBuilder sb = new StringBuilder();
int lastValue = -1;
foreach (int included in includeSet)
{
if (lastValue == -1)
{
sb.Append(included + "-");
lastValue = included;
}
else
{
if (lastValue == included - 1)
{
lastValue = included;
}
else
{
sb.Append(lastValue + ",");
sb.Append(included + "-");
lastValue = included;
}
}
}
sb.Append(lastValue);
result.Text = sb.ToString();
}
This should work faster than SortedSet trick, at least for large intervals. Idea is like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Test
{
using Pair = Tuple<int, int>; //for brevity
struct Point //point of an interval
{
public enum Border { Left, Right };
public enum Interval { Including, Excluding };
public int Val;
public int Brdr;
public int Intr;
public Point(int value, Border border, Interval interval)
{
Val = value;
Brdr = (border == Border.Left) ? 1 : -1;
Intr = (int)interval;
}
public override string ToString() =>
(Brdr == 1 ? "L" : "R") + (Intr == 0 ? "+ " : "- ") + Val;
}
class Program
{
static IEnumerable<Pair> GetInterval(string strIn, string strEx)
{
//a func to get interval border points from string:
Func<string, Point.Interval, IEnumerable<Point>> parse = (str, intr) =>
Regex.Matches(str, "[0-9]+").Cast<Match>().Select((s, idx) =>
new Point(int.Parse(s.Value), (Point.Border)(idx % 2), intr));
var INs = parse(strIn, Point.Interval.Including);
var EXs = parse(strEx, Point.Interval.Excluding);
var intrs = new int[2]; //current interval border control IN[0], EX[1]
int start = 0; //left border of a new resulting interval
//put all points in a line and loop:
foreach (var p in INs.Union(EXs).OrderBy(x => x.Val))
{
//check for start (close) of a new (cur) interval:
var change = (intrs[p.Intr] == 0) ^ (intrs[p.Intr] + p.Brdr == 0);
intrs[p.Intr] += p.Brdr;
if (!change) continue;
var In = p.Intr == 0 && intrs[1] == 0; //w no Ex
var Ex = p.Intr == 1 && intrs[0] > 0; //breaks In
var Open = intrs[p.Intr] > 0;
var Close = !Open;
if (In && Open || Ex && Close)
{
start = p.Val + p.Intr; //exclude point if Ex
}
else if (In && Close || Ex && Open)
{
yield return new Pair(start, p.Val - p.Intr);
}
}
}
static void Main(string[] args)
{
var strIN = "10-100, 200-300, 400-500, 420-480";
var strEX = "95-205, 410-420";
foreach (var i in GetInterval(strIN, strEX))
Console.WriteLine(i.Item1 + "-" + i.Item2);
Console.ReadLine();
}
}
}
So, you task could be separated to the list of subtasks:
Parse a source line of intervals to the list of objects
Concatinate intervals if they cross each over
Excludes intervals 'excludes' from 'includes'
I published my result code here: http://rextester.com/OBXQ56769
The code could be optimized as well, but I wanted it to be quite simple. Hope it will help you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication
{
public class Program
{
private const string Includes = "10-100, 200-300, 400-500 ";
private const string Excludes = "95-205, 410-420";
private const string Pattern = #"(\d*)-(\d*)";
public static void Main(string[] args)
{
var includes = ParseIntevals(Includes);
var excludes = ParseIntevals(Excludes);
includes = ConcatinateIntervals(includes);
excludes = ConcatinateIntervals(excludes);
// The Result
var result = ExcludeFromInclude(includes, excludes);
foreach (var interval in result)
{
Console.WriteLine(interval.Min + "-" + interval.Max);
}
}
/// <summary>
/// Excludes intervals 'excludes' from 'includes'
/// </summary>
public static List<Interval> ExcludeFromInclude(List<Interval> includes, List<Interval> excludes)
{
var result = new List<Interval>();
if (!excludes.Any())
{
return includes.Select(x => x.Clone()).ToList();
}
for (int i = 0; i < includes.Count; i++)
{
for (int j = 0; j < excludes.Count; j++)
{
if (includes[i].Max < excludes[j].Min || includes[i].Min > excludes[j].Max)
continue; // no crossing
//1 Example: includes[i]=(10-20) excludes[j]=(15-25)
if (includes[i].Min < excludes[j].Min && includes[i].Max <= excludes[j].Max)
{
var interval = new Interval(includes[i].Min, excludes[j].Min - 1);
result.Add(interval);
break;
}
//2 Example: includes[i]=(10-25) excludes[j]=(15-20)
if (includes[i].Min <= excludes[j].Min && includes[i].Max >= excludes[j].Max)
{
if (includes[i].Min < excludes[j].Min)
{
var interval1 = new Interval(includes[i].Min, excludes[j].Min - 1);
result.Add(interval1);
}
if (includes[i].Max > excludes[j].Max)
{
var interval2 = new Interval(excludes[j].Max + 1, includes[i].Max);
result.Add(interval2);
}
break;
}
//3 Example: includes[i]=(15-25) excludes[j]=(10-20)
if (includes[i].Min < excludes[j].Max && includes[i].Max > excludes[j].Max)
{
var interval = new Interval(excludes[j].Max + 1, includes[i].Max);
result.Add(interval);
break;
}
}
}
return result;
}
/// <summary>
/// Concatinates intervals if they cross each over
/// </summary>
public static List<Interval> ConcatinateIntervals(List<Interval> intervals)
{
var result = new List<Interval>();
for (int i = 0; i < intervals.Count; i++)
{
for (int j = 0; j < intervals.Count; j++)
{
if (i == j)
continue;
if (intervals[i].Max < intervals[j].Min || intervals[i].Min > intervals[j].Max)
{
Interval interval = intervals[i].Clone();
result.Add(interval);
continue; // no crossing
}
//1
if (intervals[i].Min < intervals[j].Min && intervals[i].Max < intervals[j].Max)
{
var interval = new Interval(intervals[i].Min, intervals[j].Max);
result.Add(interval);
break;
}
//2
if (intervals[i].Min < intervals[j].Min && intervals[i].Max > intervals[j].Max)
{
Interval interval = intervals[i].Clone();
result.Add(interval);
break;
}
//3
if (intervals[i].Min < intervals[j].Max && intervals[i].Max > intervals[j].Max)
{
var interval = new Interval(intervals[j].Min, intervals[i].Max);
result.Add(interval);
break;
}
//4
if (intervals[i].Min > intervals[j].Min && intervals[i].Max < intervals[j].Max)
{
var interval = new Interval(intervals[j].Min, intervals[j].Max);
result.Add(interval);
break;
}
}
}
return result.Distinct().ToList();
}
/// <summary>
/// Parses a source line of intervals to the list of objects
/// </summary>
public static List<Interval> ParseIntevals(string intervals)
{
var matches = Regex.Matches(intervals, Pattern, RegexOptions.IgnoreCase);
var list = new List<Interval>();
foreach (Match match in matches)
{
var min = int.Parse(match.Groups[1].Value);
var max = int.Parse(match.Groups[2].Value);
list.Add(new Interval(min, max));
}
return list.OrderBy(x => x.Min).ToList();
}
/// <summary>
/// Interval
/// </summary>
public class Interval
{
public int Min { get; set; }
public int Max { get; set; }
public Interval()
{
}
public Interval(int min, int max)
{
Min = min;
Max = max;
}
public override bool Equals(object obj)
{
var obj2 = obj as Interval;
if (obj2 == null) return false;
return obj2.Min == Min && obj2.Max == Max;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public override string ToString()
{
return string.Format("{0}-{1}", Min, Max);
}
public Interval Clone()
{
return (Interval) this.MemberwiseClone();
}
}
}
}
Lots of ways to solve this. The LINQ approach hasn't been discussed yet - this is how I would do it:
// declaring a lambda fn because it's gonna be used by both include/exclude
// list
Func<string, IEnumerable<int>> rangeFn =
baseInput =>
{
return baseInput.Split (new []{ ',', ' ' },
StringSplitOptions.RemoveEmptyEntries)
.SelectMany (rng =>
{
var range = rng.Split (new []{ '-' },
StringSplitOptions.RemoveEmptyEntries)
.Select(i => Convert.ToInt32(i));
// just in case someone types in
// a reverse range (e.g. 10-5), LOL...
var start = range.Min ();
var end = range.Max ();
return Enumerable.Range (start, (end - start + 1));
});
};
var includes = rangeFn (_string);
var excludes = rangeFn (_stringB);
var result = includes.Except (excludes).Distinct().OrderBy(r => r);
just for fun I am trying to do the change making problem - more or less. My problem is that I get doubled results and through using the debugger I found out that the compiler jumps back into the method again even when it should finish.
private static void Main(string[] args)
{
string temp = Console.ReadLine();
int input;
if (int.TryParse(temp, out input))
{
if(input == 0)
{
System.Environment.Exit(Environment.ExitCode);
}
else
{
ChangeMaking(input);
}
}
else
{
Console.WriteLine("Not a number.");
}
}
private static int ChangeMakingHelper(int input, int euro)
{
return input / euro;
}
static int[] euro = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
static int counter = 0;
static List<int[]> result = new List<int[]>();
static int[] tempResult = new int[euro.Length];
private static void ChangeMaking(int input)
{
for (int i = euro.Length -1; i >= 0; i--)
{
if(euro[i] <= input)
{
tempResult[i] = ChangeMakingHelper(input, euro[i]);
input = input - euro[i];
if((input % euro[i] != 0))
{
ChangeMaking(input % euro[i]);
}
}
}
result.Add(tempResult);
}
For example if the input is 11 then after the for-loop completes and adding tempResult to result the compiler jumps back into this part:
if((input % euro[i] != 0))
{
ChangeMaking(input % euro[i]);
}
My expected behavior for input 11 would be one array with this values {1, 0, 0, 1, 0, 0, 0, 0, 0} which I get but doubled.
The issue is that you are using static variables to pass data between function calls. Don't do that. Use return values instead.
public static void Main()
{
var result = ChangeMaking(11);
Console.WriteLine(string.Join(", ", result));
}
private static int ChangeMakingHelper(int input, int euro)
{
return input / euro;
}
static readonly int[] euro = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
private static int[] ChangeMaking(int input)
{
var result = new int[euro.Length];
for (int i = euro.Length -1; i >= 0; i--)
{
if (euro[i] <= input)
{
result[i] += ChangeMakingHelper(input, euro[i]);
input = input - euro[i];
if (input % euro[i] != 0)
{
var tempResult = ChangeMaking(input % euro[i]);
// Transfer the results to the local result array
for(int j = 0; j < result.Length; j++)
result[j] += tempResult[j];
}
// Also add a break statement here so you don't add the lower values twice!
break;
}
}
return result;
}
Fiddle: https://dotnetfiddle.net/7WnLWN
By the way, the reason you ended up with 2 arrays in your output is because you called ChangeMaking recursively, so it got called twice, so it called result.Add(tempResult) twice. Getting rid of the static variables as I have shown above fixes that.
Also, there's probably a more efficient way to do this without recursion, but that would require restructuring your algorithm a bit, so I'll leave that to you to figure out :)
I'm new to programming C#, and I've learned almost all of my information from:
http://unity3d.com/learn/tutorials/modules/beginner/scripting, youtube, this site, and many programming tutorial sites found through google.
In my monobehavior code within Unity, I am ultimately trying to make a base 12 calculator.
To do so, I need 12 numerals, I wrote a string array to represent them:
private string[] numerals = {"0","1","2","3","4","5","6","7","8","9","X","E"};
public string thisNum;
my Start and calcNum functions:
void Start ()
{
thisNum = numerals[10];
calcNum ();
}
void calcNum ()
{
print(thisNum);
}
This is great, I can type: print (thisNum);, and get back X.
But, how do I get: print (thisNum + thisNum) to return 18?
I know it's not an integer, therefore it can not add 2 strings to get a sum, you instead get: XX.
So then, how do I represent X as this many:
o o o, o o o, o o o, o
and not just the letter X. I reset this project about 6 times now.
I was thinking of for-loops or if (X) than 10, but, I always end up using base 10 to represent numbers, which is kind of lame.
I just need a little push to get going in the right direction, and would really appreciate the help,
thank you.
This might help.
Start with an array of characters rather than strings.
var numerals = new []
{
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'X', 'E',
};
Create a couple of dictionaries to return the base 10 value of each numerals and to perform the reverse look-up.
var nis =
numerals
.Select((n, i) => new { n, i })
.ToArray();
var n2i = nis.ToDictionary(_ => _.n, _ => _.i);
var i2n = nis.ToDictionary(_ => _.i, _ => _.n);
Then to convert between base 10 and base 12 you need a couple of helper functions.
Func<int, IEnumerable<char>> getReversedNumerals = null;
getReversedNumerals = n =>
{
IEnumerable<char> results =
new [] { i2n[n % 12], };
var n2 = n / 12;
if (n2 > 0)
{
results = results.Concat(getReversedNumerals(n2));
}
return results;
};
Func<IEnumerable<char>, int, int> processReversedNumerals = null;
processReversedNumerals = (cs, x) =>
cs.Any()
? x * n2i[cs.First()]
+ processReversedNumerals(cs.Skip(1), x * 12)
: 0;
Now you can define the conversion functions in terms of the helpers.
Func<int, string> convertToBase12 =
n => new String(getReversedNumerals(n).Reverse().ToArray());
Func<string, int> convertToBase10 =
t => processReversedNumerals(t.ToCharArray().Reverse(), 1);
And finally you can perform conversions:
var b10 = convertToBase10("3EX2"); //6890
var b12 = convertToBase12(6890); //3EX2
It depends on how you're taking in, and storing the values.
Assuming you're storing them as their base 12 string, you'll need conversion methods back and forth between the integer base10 value it represents, and the string value that you're using to represent it.
public String Base12Value(int base10)
{
String retVal = "";
while (base10 > 0)
{
//Grab the mod of the value, store the remainder as we build up.
retVal = (base10 % 12).ToString() + retVal;
//remove the remainder, divide by 12
base10 = (base10 - (base10 % 12)/12);
}
return retVal;
}
public int Base10Value(String base12)
{
int retVal = 0;
for (int i = 1; i <= base12.Length; i++)
{
int tmpVal = 0;
char chr = base12[base12.Length-i];
//Grab out the special chars;
if (chr == 'X')
{
tmpVal = 10;
} else if (chr == 'E')
{
tmpVal = 11;
}
else
{
tmpVal = int.Parse(chr.ToString());
}
//Times it by the location base.
retVal += tmpVal * (10 ^ (i - 1));
}
return retVal;
}
So you can then do things like
print(Base12Value(Base10Value(thisNum) + Base10Value(thisNum)));
which is a tad clunky, but gets the job done.
Oh, even though some already put it, here is mine since I worked on it.
using System;
using System.Text;
using System.Collections.Generic;
public class Base12
{
public static void Main()
{
Base12 X = new Base12(10);
Base12 X2 = new Base12(10);
Base12 XX = X + X2;
Console.WriteLine(XX); // outputs 18
}
public int DecimalValue { get; set; }
public readonly char[] Notation = new char[] {'0', '1' , '2' , '3', '4', '5' , '6', '7', '8', '9', 'X', 'E'};
public Base12(int x)
{
DecimalValue = x;
}
public override string ToString()
{
List<char> base12string = new List<char>();
int copy = DecimalValue;
while(copy > 0)
{
int result = copy % 12;
base12string.Add(Notation[result]);
copy = copy / 12;
}
StringBuilder str = new StringBuilder();
for(int i = base12string.Count - 1; i >= 0; i--)
{
str.Append(base12string[i]);
}
return str.ToString();
}
public static Base12 operator+(Base12 x, Base12 y)
{
return new Base12(x.DecimalValue + y.DecimalValue);
}
// Overload other operators at your wish
}
Here's my implementation of your problem. I have to say, this is one fun project!
I didnt want to use any decimal values, because I thought that was cheating. I only used decimals as de index for the list.
using System;
using System.Collections.Generic;
using System.Linq;
class Base12
{
static IList<char> values = new List<char>{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'E' };
public string Value { get; set; }
public Base12(string value)
{
this.Value = value;
}
public static Base12 operator +(Base12 x, Base12 y)
{
var xparts = x.Value.ToArray();
var yparts = y.Value.ToArray();
int remember = 0;
string result = string.Empty;
for (int i = 0; i < Math.Max(yparts.Length, xparts.Length) ;i++)
{
int index = remember;
if (i < xparts.Length)
{
index += values.IndexOf(xparts[xparts.Length - i - 1]);
}
if (i < yparts.Length)
{
index += values.IndexOf(yparts[yparts.Length - i - 1]);
}
if (index > 11)
{
index -= 12;
remember = 1;
}
else
{
remember = 0;
}
result = values[index] + result;
}
if (remember > 0)
{
result = values[remember] + result;
}
return new Base12(result);
}
public static implicit operator Base12(string x)
{
return new Base12(x);
}
public override string ToString()
{
return this.Value;
}
}
And here is how you might use it:
Base12 x = "X";
Base12 y = "X";
Base12 z = x + y;
Debug.Print(z.ToString());
// returns 18
Base12 x = "X12X";
Base12 y = "X3";
Base12 z = x + y;
Debug.Print(z.ToString());
// returns X211
So lets say I have an array of strings:
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
Lets say I have to loop through these values and do the following operations:
Round it to the nearest even number (if it's a double only)
After rounding remove the fractional part of the number.
If the number is negative remove the sign.
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
for (int i = 0; i < values.Length; i++)
{
file[i].Value = ((Int32)Math.Abs(Math.Round(Double.Parse(values[i]), MidpointRounding.ToEven))).ToString();
}
which is basically the same as doing this:
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
for (int i = 0; i < values.Length; i++)
{
String strValue = values[j];
Double dblValue = Double.Parse(strValue);
Double dblRoundedValue = Double.Parse(dblValue);
Int32 intValue = (Int32)dblRoundedValue;
Int32 intAbsValue = Math.Abs(intValue);
String finalValue = intAbsValue.ToString();
file[i].Value = finalValue;
}
There could be over a million values in that array so is there a way to make this process more efficient?
This operation is inherently parallelizable (if that's a word). A Parallel.ForEach loop, parallel Linq pipeline, or something similar, would improve execution time.
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
var file = values.AsParallel()
.Select(s => Double.Parse(s))
.Select(d => (int)Math.Round(d))
.Select(i => Math.Abs(i).ToString())
.ToArray();
Well, I tried some things, and using a combination of older-than-the-supernova's suggestion of checking for a decimal point in the string before considering if it was necessary to parse it to a Double, and Andrew Cooper's suggestion of using Parallel.For I got results of
Init...Done
Simple
20024
LookingAtString
8082
ParallelConvertLookingAtString
3559
Simple
19552
LookingAtString
7985
ParallelConvertLookingAtString
3595
with the following code...
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
const int nNumbers = 20000000;
static string[] values = new string[nNumbers];
static string[] file = new string[nNumbers];
static Random rand = new Random();
// Create some sample data.
static void Init()
{
string sgn = "";
for (int i = 0; i <= nNumbers - 1; i++)
{
sgn = rand.Next(51) == 1 ? "-" : "";
if (rand.Next(4) == 1)
{
values[i] = sgn + (rand.NextDouble() * 100).ToString();
}
else
{
values[i] = sgn + rand.Next(100);
}
}
}
static void ConvertSimple()
{
for (int i = 0; i <= nNumbers - 1; i++)
{
file[i] = Math.Abs(Math.Round(double.Parse(values[i]), MidpointRounding.ToEven)).ToString();
}
}
static void ConvertLookingAtString()
{
for (int i = 0; i <= nNumbers - 1; i++)
{
if (values[i].IndexOf('.') >= 0)
{
file[i] = Math.Abs(Math.Round(double.Parse(values[i]), MidpointRounding.ToEven)).ToString();
}
else
{
file[i] = values[i].TrimStart('-');
}
}
}
static void ParallelConvertLookingAtString()
{
Parallel.For(0, nNumbers, i =>
{
if (values[i].IndexOf('.') >= 0)
{
file[i] = Math.Abs(Math.Round(double.Parse(values[i]), MidpointRounding.ToEven)).ToString();
}
else
{
file[i] = values[i].TrimStart('-');
}
});
}
static void Main()
{
Console.Write("Init...");
Init();
Console.WriteLine("Done");
Stopwatch sw = new Stopwatch();
// run each test twice
for (int testNum = 0; testNum < 2; testNum++)
{
sw.Reset();
sw.Start();
ConvertSimple();
sw.Stop();
Console.WriteLine("Simple\n" + sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
ConvertLookingAtString();
sw.Stop();
Console.WriteLine("LookingAtString\n" + sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
ParallelConvertLookingAtString();
sw.Stop();
Console.WriteLine("ParallelConvertLookingAtString\n" + sw.ElapsedMilliseconds.ToString());
}
Console.ReadLine();
}
}
}
Please note that that is using twenty million samples rather than the one million-ish you suggest, and about one third of the values are fractional, and about two percent are negative. You didn't give the expected fraction of those, so I made something up.
Some bits may be non-optimal C# because I converted it from VB. Run on an Intel Core i7 920 with 6GB of RAM, compiled to run on x64.
Edit: Oh yeah, so my answer is the above ParallelConvertLookingAtString method.
A couple of ideas:
If you have a million values and you expect a lot of repeats then you can build up a Hashtable of previous results that you can reuse.
I tested this and it is much faster if you have say all the elements the same. It is probably faster with a small number of distinct elements. However if the elements are all different it uses a lot of memory and you would want to cap it once the Hash gets too big.
If you are sure of the formatting locale, then you can do this:
A string is a character array. Instead of converting to double, iterate though the characters until you reach the first "." then take the next digit. Convert the text (ignoring any -) before the . to an integer. If the value after the . is 5,6,7,8,9 then add 1 to that integer.
Example:
Input: "-1002.55"
Text between - and . is: "1002"
Converted to int this is: 1002
Character immediately after . is: "5"
Result: 1002 + 1 = 1003
That is for round to nearest. If you need to round to nearest even number look at digit immediately before . and if there is one after, and the digit before . is 1,3,5,7,9 then add to the final number.
Double dblValue;
Double dblRoundedValue;
Int32 intValue;
Int32 intAbsValue;
String finalValue;
for (int i = 0; i < values.Length; i++)
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
//dblValue = Double.Parse(strValue);
//dblRoundedValue = Double.Parse(dblValue);
//intValue = (Int32)dblRoundedValue;
intValue = (Int32)(Double.Parse(strValue));
}
//intValue = Math.Abs(intValue);
//finalValue = intValue .ToString();
file[i].Value = (Math.Abs(intValue)).ToString();
}
But I don't understand the two Double.Parse.
Put em back in if you need them
And convert this to a parallel
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 16;
Parallel.For(0, values.Length, parallelOptions, i =>
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
intValue = (Int32)(Double.Parse(strValue));
}
if (intValue < 0) intValue = -intValue;
file[i].Value = intValue.ToString();
});
I think grunge will be the fastest
string[] values = new string[] { "1", "2", "3", "1.5", "56.5", "8" };
string[] files = new string[values.Length];
HashSet<char> lt5 = new HashSet<char> {'0','1','2','3','4'};
bool haveDecimal;
bool haveDecimalConfirmed;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.Length; i++)
{
sb.Clear();
haveDecimal = false;
haveDecimalConfirmed = false;
foreach(char c in values[i])
{
if (haveDecimal)
{
if (lt5.Contains(c))
{
files[i] = sb.ToString();
}
else
{
files[i] = (Int32.Parse(sb.ToString()) + 1).ToString();
}
haveDecimalConfirmed = true;
break;
}
else if (c == '.')
{
haveDecimal = true;
continue;
}
if (c == '-') continue;
sb.Append(c);
}
if (!haveDecimalConfirmed) files[i] = sb.ToString();
}
Assuming you mean "even number" to mean "nearest integer" and not "integer multiple of 2".
Assuming you mean to remove negative sign from all numbers as opposed to only numbers you have rounded.
You assume that your input strings are always valid Double format, is that reasonable? Assuming there are no numbers formatted with scientific notation, examine each input string for a decimal point, its faster than parsing and a test for a fractional part. No decimal point, no rounding needed.