c# - add or subtract sets of ranges like "1,2,4,10-25,40+" + "3,11,26" -


i writing c# program user can enter range of values in sets, "1,2,4,10-25,40+" items can individual number, range 10-25, , range infinity 25+. ranges must @ least 2 apart, cannot 10-11, should 10,11. need functions add , subtract them.

for example, "1,2,4,10-25,40+" + "3,11,26" return "1-4,10-26,40+"

i using strings example purposes here, assume data parsed objects 3 categories: single number, range, infinite range. i'm not asking how parse strings.

i writing own set of functions, , seeing there lot of complexity due fact ranges must 2 apart. know there functions test if number in 1 range, , easy build routine test if number in set of ranges.

but how can add or subtract sets, , simplify them not 11,12,13 11-13 instead? not trivial. open solution , can import libraries if it's required.

here's little function wrote out...fiddle

snippet

private static list<string> simplify(list<string> inputs) {     var simplelist = new list<int>();     var retval = new list<string>();     bool infinity = false;      foreach (string input in inputs)     {         if (string.isnullorempty(input))             continue;          if (input.split('-').length > 1)         {             int min = int.parse(input.split('-')[0].trim());             int max = int.parse(input.split('-')[1].trim());              // inclusive             simplelist.addrange(enumerable.range(min, max - min + 1));              continue;         }          if (input.trim().endswith("+"))         {             infinity = true;             simplelist.add(int.parse(input.trim().trim('+')));         } else simplelist.add(int.parse(input.trim()));     }      simplelist.sort();      (int = 0; < simplelist.count; i++)     {         int currentval = simplelist[i];         int q = i;         while (q < simplelist.count)         {             if (q != simplelist.count - 1 && simplelist[q] + 1 == simplelist[q + 1])             {                 q++;                 continue;             }             if (currentval == simplelist[q])             {                 retval.add(currentval.tostring());                 = q;                 break;             }             if (currentval + 1 == simplelist[q])             {                 retval.add(currentval.tostring());                 retval.add(simplelist[q].tostring());                 = q;                 break;             }             retval.add(currentval + "-" + simplelist[q]);             = q;             break;         }     }      if (infinity)         retval[retval.count - 1] = retval[retval.count - 1] + "+";      return retval; } 

test case:

var inputs = new list<string> {"11", "12", "13", "14-18", "2-3", "25", "82+", "9"};  var simplifiedlist = simplify(inputs);  foreach (string input in simplifiedlist) {     console.writeline(input); } 

output:

2 3 9 11-18 25 82+ 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -