Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Created November 28, 2018 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/3b326d5f75a732a73636d64fe895f42f to your computer and use it in GitHub Desktop.
Save CompSciRocks/3b326d5f75a732a73636d64fe895f42f to your computer and use it in GitHub Desktop.
Solution for 2016 AP Computer Science Free Response LogMessage - https://compsci.rocks/logmessage-solution/
public LogMessage( String message ) {
int pos = message.indexOf(":");
machineId = message.substring(0, pos);
description = message.substring(pos + 1);
}
public LogMessage( String message ) {
String[] spl = message.split(":");
machineId = spl[0];
description = spl[1];
}
public boolean containsWord( String keyword ) {
if (keyword.equals(description))
return true;
else if (description.indexOf(keyword + " ") == 0)
return true;
else if (description.indexOf(" " + keyword + " ") >= 0)
return true;
else if (description.length() > keyword.length()
&& description.indexOf(" " + keyword) == description.length() - keyword.length() - 1)
return true;
return false;
}
public boolean containsWord( String keyword ) {
return description.matches(".*(\\s|^)" + keyword + "(\\s|$).*");
}
public boolean containsWord( String keyword ) {
return (" " + description + " ").indexOf(" " + keyword + " ") >= 0;
}
public List<LogMessage> removeMessages( String keyword ) {
List<LogMessage> out = new ArrayList<>();
for (int i=messageList.size() - 1; i>=0; i--) {
if (messageList.get(i).containsWord(keyword)) {
out.add(0, messageList.remove(i));
}
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment