C# gic将省略一些。但那正是他想要的…@Reddy-对不起,我还是不明白要求是什么。这听起来很奇怪。是的,确实很奇怪 List<string> history = new List<string>(){ "AA", "BB", "CC
C# gic将省略一些。但那正是他想要的…@Reddy-对不起,我还是不明白要求是什么。这听起来很奇怪。是的,确实很奇怪 List<string> history = new List<string>(){ "AA", "BB", "CC,c#,.net,linq,list,overlapping,C#,.net,Linq,List,Overlapping,gic将省略一些。但那正是他想要的…@Reddy-对不起,我还是不明白要求是什么。这听起来很奇怪。是的,确实很奇怪 List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" }; List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"}; AA, BB, CC, A
gic将省略一些。但那正是他想要的…@Reddy-对不起,我还是不明白要求是什么。这听起来很奇怪。是的,确实很奇怪
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};
AA, BB, CC, AA, DD, EE, FF, AA
List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };
var result = history.Concat(potentialNew.Where(x => !history.Contains(x)).ToList());
string lastItem = history.Last();
int lastIndexToCheck=history.Count-2,i=0;
for (; i < potentialNew.Count - 1; i++)
{
if (potentialNew[i] == lastItem && potentialNew[i - 1] == history[lastIndexToCheck])
{
break;
}
}
history.AddRange(potentialNew.Skip(i+1).ToList());
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF" };
// make lists equal length
foreach(var x in history.ConcatOverlap(potentialNew)){
Console.WriteLine(x);
}
}
}
public static class Ext{
public static IEnumerable<string> ConcatOverlap(this List<string> history, List<string> potentialNew){
var hleng = history.Count();
var pleng = potentialNew.Count();
if(pleng > hleng) history = history.Concat(Enumerable.Range(1, pleng - hleng).Select(x => string.Empty)).ToList();
if(hleng > pleng) potentialNew = Enumerable.Range(1, hleng - pleng).Select(x => string.Empty).Concat(potentialNew).ToList();
var zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
var count = 0;
var max = pleng > hleng ? pleng : hleng;
Console.WriteLine("Max " + max);
while(zipped.Any(x => !x.Equal) && count < max - 1){
count++;
potentialNew.Insert(0,string.Empty);
history.Add(string.Empty);
zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
}
return zipped.Select(x => string.IsNullOrEmpty(x.First) ? x.Next : x.First);
}
}
public static IEnumerable<T> ConcatOverlap<T>(this IEnumerable<T> head, IEnumerable<T> tail){
var skip = 0;
var hLen = head.Count();
while(head.Skip(skip).Zip(tail, (a,b) => a.Equals(b)).Any(x => !x) && skip < hLen){
skip++;
}
return head.Take(skip).Concat(tail);
}
var history = new List<string>() { "AA", "BB", "CC", "AA" };
var potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };
// Get the min. number of items to compare that 2 lists
for (int count = Math.Min(history.Count(), potentialNew.Count()); count >= 0; count--)
{
// Get the items from the back of history list, and get the items from potential list
// Compare them by SequenceEqual()
if (history.Skip(history.Count() - count).Take(count).SequenceEqual(potentialNew.Take(count)))
{
// Add the items to the result if found. It must be the greatest common part
return history.Concat(potentialNew.Skip(count));
}
}
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};
potentialNew.Aggregate(history, (h, p) =>
{
if (!h.Skip(h.Count - 2).Contains(p))
{
h.Add(p);
}
return h;
});
AA
BB
CC
AA
DD
EE
FF
AA
List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF", "AA" };
var result = ProcessChatLog(history,potentialNew);
//pass these two list to a function to process the chat log
public List<string> ProcessChatLog(List<string> history, List<string> potentialNew)
{
var lastChat = history.Last();
var lastChatIndex = history.Count - 1;
var allIndexWithLastChat = potentialNew.Select((c, i) => new { chat = c, Index = i })
.Where(x => x.chat == lastChat)
.Select(x => x.Index).Reverse().ToList();
List<int> IndexToClear = new List<int>();
bool overlapFound = false;
foreach (var index in allIndexWithLastChat)
{
if (!overlapFound)
{
int hitoryChatIndex = lastChatIndex;
IndexToClear.Clear();
for (int i = index; i > -1; i--)
{
if (potentialNew[i] == history[hitoryChatIndex])
{
IndexToClear.Add(i);
if (i == 0)
{
overlapFound = true;
break;
}
hitoryChatIndex--;
}
else
{
break;
}
}
}
else
{
IndexToClear.Clear();
break;
}
}
if(IndexToClear.Count >0)
{
potentialNew.RemoveRange(IndexToClear.Min(), IndexToClear.Count);
}
return history.Concat(potentialNew).ToList();
}
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB","AA", "CC", "AA" }
potentialNew = { "AA","CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","AA","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "CC", "AA", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "CC", "AA", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = {"AA", "CC", "AA", "DD", "EE", "FF", "AA" }
Result = { "AA", "BB","CC", "AA", "CC", "AA", "DD", "EE", "FF", "AA" }