JavaRush /Java Blog /Random EN /Option for solving the problem level 19 task 16 (monitori...
Anton Stezhkin
Level 41

Option for solving the problem level 19 task 16 (monitoring changes)

Published in the Random EN group

Background:

I solved this problem for a very long time . In it it was necessary to compare 2 versions of the file and find changes. I decided to extract the contents of the files in the form of arrays and compare the arrays. Then I was stupid for a long time and made mistakes and in the end I drew arrays on a piece of paper in a box. Actually, before this I looked at another solution. But it’s so complicated that I couldn’t master it :) In addition, there were 2 different algorithms in case the old file is longer and if the new file is longer. I did not like it.

The gist of my solution:

There are 2 identical arrays. Throughout the text I will call them “new array” and “old array”. And new elements can be inserted into each of them. Those. The reference array is considered to be the one corresponding to the contents of the old file with all deletions. The contents of the old and new file are treated as a reference with inserts. We go through both arrays (the contents of the old and the new) in a loop. And if we detect an insertion in one of them, then we skip one step so that the same elements of the compared arrays are again nearby.

Algorithm:

Variables: i - index of the array cell with the contents of the OLD file. nI - index of the array cell with the contents of the NEW file. If the elements of the arrays differ, we write them into temporary variables: oldMismatch - an element from a stacked array newMismatch - an element from a new array When iterating over array elements, the following cases are possible:
  1. the oldMismatch and newMismatch variables are empty. The elements in the two arrays are the same. Write Type.SAME to the list. Go ahead.

  2. the oldMismatch and newMismatch variables are empty. The elements in the two arrays are NOT the same. We write the value from the old one to oldMismatch, from the new one to newMismatch. Go ahead.

  3. the variables oldMismatch and newMismatch are NOT empty. We compare them with the current elements of the array.

    Let's draw conclusions. We write the findings to a list (lines variable). We skip the loop step for one of the arrays.

    1. 3.1 oldMismatch is equal to the current element of the NEW array. This means that a line was added to the file.

      The value of this string is stored in newMismatch. Let's write it down like that.

      lines.add(new LineItem(Type.ADDED, newMismatch));
      lines.add(new LineItem(Type.SAME, oldMismatch));

      Since there is an additional element in the array containing the contents of the new file, you need to shift the new array 1 element forward relative to the old one.

      Therefore, the OLD array skips 1 loop step.

      i--;

    2. 3.2 newMismatch is equal to the current element of the OLD array. This means that a line has been removed from the file. Let's write it down.

      lines.add(new LineItem(Type.REMOVED, oldMismatch));
       lines.add(new LineItem(Type.SAME, newMismatch));

      There is an additional element in the OLD array. This means that the NEW array skips 1 loop step.

      nI--;

  4. Processing the end of the array. And now we have come to the end of the old array. There are several possible situations

    1. 4.1 - ArrayIndexOutOfBoundsException - the new array is shorter than the old one. We record that the last line of the file was deleted.

    2. 4.2 - The last element of the new array remains, not covered by our attention. We record it as added.

    3. 4.3 - The variables oldMismatch and newMismatch are not empty. We write down:

      lines.add(new LineItem(Type.ADDED, newMismatch));
      lines.add(new LineItem(Type.SAME, oldMismatch));
PS - don’t forget to reset variables and keep track of the nI variable.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION