Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Found many section of times intersect (can edit)
//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; using System.Globalization; namespace Rextester { public class Program { class TimeRange { public DateTime Start{get;set;} public DateTime? End{get;set;} public string Status{get;set;} } public static void Main(string[] args) { //Your code goes here Console.WriteLine("HoaND:_"); var provider = new CultureInfo("de-DE"); //var d = DateTime.ParseExact("08.05.2016", "d", provider); //Console.WriteLine(d); /* *Requirement* Test 1: => input (dd.mm.yyyy): t1 = 08.05.2016 -> 20.05.2016 t2 = 17.05.2016 -> 25.05.2016 t3 = 27.05.2016 -> 28.05.2016 => output: t1 = 08.05.2016 -> 17.05.2016, Status = normal t2 = 17.05.2016 -> 20.05.2016, Status = overlap t3 = 20.05.2016 -> 25.05.2016, Status = normal t4 = 27.05.2016 -> 28.05.2016, Status = normal Test 2: => input: t1 = 05.05.2016 -> 20.05.2016 t2 = 07.05.2016 -> 15.05.2016 t3 = 17.05.2016 -> 25.05.2016 => output: t1 = 05.05.2016 -> 07.05.2016, Status = normal t2 = 07.05.2016 -> 15.05.2016, Status = overlap t3 = 15.05.2016 -> 17.05.2016, Status = normal t4 = 17.05.2016 -> 20.05.2016, Status = overlap t5 = 20.05.2016 -> 25.05.2016, Status = normal Test3: => input: t1 = 05.05.2016 -> 07.05.2016 t2 = 15.05.2016 -> 20.05.2016 t3 = 22.05.2016 -> 25.05.2016 => output: t1 = 05.05.2016 -> 07.05.2016, Status = normal t2 = 15.05.2016 -> 20.05.2016, Status = normal t3 = 22.05.2016 -> 25.05.2016, Status = normal */ var list = new List<TimeRange>{ new TimeRange{ Start = DateTime.ParseExact("21.10.2016", "d", provider), End = DateTime.ParseExact("21.10.2016", "d", provider), Status = "" }, /* new TimeRange{ Start = DateTime.ParseExact("10.05.2016", "d", provider), //End = DateTime.ParseExact("07.05.2016", "d", provider), End = null, Status = "" }, new TimeRange{ Start = DateTime.ParseExact("01.05.2016", "d", provider), End = DateTime.ParseExact("15.05.2016", "d", provider), Status = "" } */ }; ///* Console.WriteLine("=> Input: "); foreach(var item in list){ Console.WriteLine(string.Format("{0} -> {1}", item.Start.ToShortDateString(), item.End != null ? item.End.Value.ToShortDateString() : "NULL", item.Status )); } //*/ //Logic here list.ForEach(x=>{ x.End = x.End ?? DateTime.MaxValue; }); var lst = new List<DateTime>(); foreach(var item in list){ lst.Add(item.Start); lst.Add(item.End ?? DateTime.MaxValue); } lst = lst.OrderBy(t=>t).ToList(); /* foreach(var item in lst){ Console.WriteLine(string.Format("{0}",item)); } */ var rs1 = new List<TimeRange>(); for(var i = 1; i< lst.Count; i++){ var temp = new TimeRange{Start = lst[i-1], End = lst[i]}; var tb = temp.Start.AddDays((temp.Start.Date == temp.End.Value.Date ? 0: 1)); switch(list.Where(t=> (tb.Date >= t.Start.Date && tb.Date <= t.End.Value.Date)).Count()) { case 0: temp.Status = "blank"; break; case 1: temp.Status = "normal"; break; default: temp.Status = "overlap"; break; } rs1.Add(temp); } rs1.RemoveAll(x=>x.Status == "blank"); var rs2 = new List<TimeRange>(); rs1.ForEach(x=>{ if(rs2.Find(t=>t.Start.Date == x.Start.Date && t.End.Value.Date == x.End.Value.Date) == null){ rs2.Add(x); } }); rs2.RemoveAll(x=>x.Start.Date == x.End.Value.Date && (rs2.Find(t=>t.Start.Date != t.End.Value.Date && (t.Start.Date == x.Start.Date || t.End.Value.Date == x.End.Value.Date) && t.Status == x.Status) != null)); Console.WriteLine("=> Output: "); rs2.ForEach(x=>{ x.End = x.End == DateTime.MaxValue ? null : x.End; }); for (var i = 1; i < rs2.Count; i++) { if (rs2[i - 1].End.Value.Date == rs2[i].Start.Date && rs2[i - 1].Status != rs2[i].Status) { if (rs2[i].Status == "overlap") rs2[i - 1].End = rs2[i].Start.AddDays(-1); } } foreach(var item in rs2){ Console.WriteLine(string.Format("{0} -> {1},Status={2}", item.Start.ToShortDateString(), item.End != null ? item.End.Value.ToShortDateString() : "NULL", item.Status )); } } } }
run
|
edit
|
history
|
help
0
ExtensionDateTime
sdfghjikujyhtgrfedwcdf
Pos and Neg
fracOp
hackton
D19
Captcha
HashSet implementation
QuickSort
número poderoso