Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
var rangeString = "-6,-3--1,3-5,7-11,14,15,17-20";
var matches = Regex.Matches(rangeString, @"(?<f>-?\d+)-(?<s>-?\d+)|(-?\d+)");
var values = new List<string>();
foreach (var m in matches.OfType<Match>())
{
if (m.Groups[1].Success)
{
values.Add(m.Value);
continue;
}
var start = Convert.ToInt32(m.Groups["f"].Value);
var end = Convert.ToInt32(m.Groups["s"].Value) + 1;
values.AddRange(Enumerable.Range(start, end - start).Select(v => v.ToString()));
}
Console.WriteLine(string.Join(", ", values));
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace RangeExpansion {
internal static class StringExtensions {
internal static IEnumerable<int> ExpandRange(this string s) {
return s.Split(',')
.Select(rstr => {
int start;
if (int.TryParse(rstr, out start))
return new {Start = start, End = start};
var istr = new string(("+-".Any(_ => rstr[0] == _)
? rstr.Take(1).Concat(rstr.Skip(1).TakeWhile(char.IsDigit))
: rstr.TakeWhile(char.IsDigit)
).ToArray());
rstr = rstr.Substring(istr.Length + 1, (rstr.Length - istr.Length) - 1);
return new {Start = int.Parse(istr), End = int.Parse(rstr)};
}).SelectMany(_ => Enumerable.Range(_.Start, _.End - _.Start + 1));
}
}
internal static class Program {
private static void Main() {
const string RANGE_STRING = "-6,-3--1,3-5,7-11,14,15,17-20";
var values = RANGE_STRING.ExpandRange().ToList();
var vstr = string.Join(", ", values.Select(_ => _.ToString()));
Console.WriteLine(vstr);
}
}
}