The validator said me:
"Be sure that the lines list has the ADDED label with the required lines in the correct places."
I checked the program with the 22 test cases shown in the attached image and my program gives the outputs shown in red.
(I made sure that this program would never be able to give consecutive ADDED/REMOVED pairings. And in my opinion it tags the lines correctly with the labels.)
1) Which case violates CodeGym's (unspoken?secret?) requirements, and how?
What would be the correct output for the given incorrect case so that the validator can accept it?
2) Or an other option: what case did I forget to test that gives incorrect output?
package com.codegym.task.task19.task1916;
/*
Tracking changes
*/
import java.util.*;
import java.io.*;
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static void main(String args[]) throws Exception {
ArrayList<String> origiList = new ArrayList<>();
ArrayList<String> updatedList = new ArrayList<>();
BufferedReader readConsole = new BufferedReader(new InputStreamReader(System.in));
String fileNameOrigi = readConsole.readLine();
String fileNameUpdated = readConsole.readLine();
readConsole.close();
BufferedReader origiFileRead = new BufferedReader(new FileReader(fileNameOrigi));
String line;
while ((line = origiFileRead.readLine()) != null) {
if (line.isEmpty()) break;
origiList.add(line);
}
origiFileRead.close();
BufferedReader updatedFileRead = new BufferedReader(new FileReader(fileNameUpdated));
while ((line = updatedFileRead.readLine()) != null) {
if (line.isEmpty()) break;
updatedList.add(line);
}
updatedFileRead.close();
if (origiList.size()==0 && updatedList.size()>0) {
added(updatedList.get(0));
System.out.println(lines);
return;
}
if (origiList.size() > 0 ) {
int remove = 0;
for (int i=0; i<origiList.size(); i++) {
if (updatedList.size() > i-remove) {
if (origiList.get(i).equals(updatedList.get(i-remove))) {
same(origiList.get(i));
/* */ if ((origiList.size() <= i+1) && (updatedList.size() > i-remove+1)) {
added(updatedList.get(i-remove+1)); // System.out.println("itt-if");
}
else { /* System.out.println("itt-else, "+remove); */ }
}
else {
if (origiList.size() > i+1) {
if (updatedList.size() > i-remove+1) {
if (origiList.get(i+1).equals(updatedList.get(i-remove))) {
removed(origiList.get(i));
same(origiList.get(i+1));
remove++;
i++;
}
else {
if (origiList.get(i).equals(updatedList.get(i-remove+1))) {
added(updatedList.get(i-remove));
same(origiList.get(i));
remove--;
}
else { removed(origiList.get(i));
break;
}
}
}
else {
if (origiList.get(i+1).equals(updatedList.get(i-remove))) {
removed(origiList.get(i));
same(origiList.get(i+1));
if (origiList.size() > i+2) {
removed(origiList.get(i+2));
break;
}
else break;
}
else {removed(origiList.get(i)) ; break; }
}
}
else {
if (updatedList.size() > i-remove+1) {
if (origiList.get(i).equals(updatedList.get(i-remove+1))) {
added(updatedList.get(i-remove));
same(origiList.get(i));
if (updatedList.size() > (i-remove+2)) {
added(updatedList.get(i-remove+2));
break;
}
else break;
}
else { removed(origiList.get(i)); break;}
}
else {removed(origiList.get(i)); break;}
}
}
}
else {
removed(origiList.get(i));
break;
}
}
}
// System.out.println(lines);
}
public static void added(String line) {
lines.add(new LineItem(Type.ADDED, line));
}
public static void removed(String line) {
lines.add(new LineItem(Type.REMOVED, line));
}
public static void same(String line) {
lines.add(new LineItem(Type.SAME, line));
}
public static enum Type {
ADDED, // New line added
REMOVED, // Line deleted
SAME // No change
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
// @Override
// public String toString() {
// return type.toString()+"-"+line;
// }
}
}