0

I don't know if my title is correct.

I have a list

\1925\10\04\issue1
\1925\10\05\issue1
\1925\10\07\issue1
\1925\10\10\issue1
\1925\10\11\issue1
\1925\10\12\issue1
\1925\10\13\issue1
\1925\10\14\issue1
\1925\10\15\issue1
\1925\10\17\issue1
\1925\10\18\issue1
\1925\10\19\issue1

And what i want to do in the list is became

\1925\10\04\issue1
\05\issue1
\07\issue1
\10\issue1
\11\issue1
\12\issue1
\13\issue1
\14\issue1
\15\issue1
\17\issue1
\18\issue1
\19\issue1

I need it to be dynamic.

There may be instance that i have a list like this

\1925\10\04\issue1
\1925\10\04\issue2
\1925\10\04\issue3
\1925\10\04\issue4

And the output is like this

\1925\10\04\issue1
\issue2
\issue3
\issue4

So far i'm using diff match patch.

https://github.com/google/diff-match-patch/wiki/Language:-C%23

And here is my code.

diff_match_patch dmp = new diff_match_patch();
            var d = dmp.diff_main(@"\1925\10\14\issue1", @"\1925\10\05\issue1");

            //dmp.diff_cleanupEfficiency(d);

            foreach (var item in d)
            {
                Console.WriteLine($"text {item.text} operation {item.operation}");
            }

But is there a better way of doing this? or faster way

1 Answer 1

2

assuming you have the input as List<string> input then this code should work:

  var splittet = input.Select(i => i.Split("\\".ToCharArray(),  StringSplitOptions.RemoveEmptyEntries));

  Action<string[], int> print = (string[] lst, int index) => Console.WriteLine("\\" + string.Join("\\", lst.Skip(index)));

  splittet.Aggregate(new string[] { },
    (common, item) =>
    {
      var index = Enumerable.Range(0, Math.Min(common.Length, item.Length)).FirstOrDefault(i => common[i] != item[i]);
      print(item, index);
      return item;
    }
    );

So given the input

var input = new List<string> { @"\1925\10\04\issue1",
@"\1925\10\05\issue1",
@"\1925\10\07\issue1",
@"\1925\10\10\issue1",
@"\1925\10\11\issue1",
@"\1925\10\12\issue1",
@"\1925\10\04\issue1",
@"\1925\10\04\issue2",
@"\1925\10\04\issue3",
@"\1925\10\04\issue4"};

this is the output:

\1925\10\04\issue1
\05\issue1
\07\issue1
\10\issue1
\11\issue1
\12\issue1
\04\issue1
\issue2
\issue3
\issue4

Some explanation:

First instead of working with a list of string, I split it up to a list of tokens.

Then I defined some print-action. you could instead add the result to an output-list or do whatever. In this case, it is just writing to console

then the list is aggregated. the aggreagtor starts with an empty string-array. then it tries to find the first index, where the first item differs from the emtpy list and prints all parts from this index on. and then the first index is returned to the aggregator. The aggregator then compares this first item with the second item, finds the first index where the parts differ and prints the parts from there on etc etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.