Created
November 28, 2018 20:20
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public LogMessage( String message ) { | |
int pos = message.indexOf(":"); | |
machineId = message.substring(0, pos); | |
description = message.substring(pos + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public LogMessage( String message ) { | |
String[] spl = message.split(":"); | |
machineId = spl[0]; | |
description = spl[1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean containsWord( String keyword ) { | |
return description.matches(".*(\\s|^)" + keyword + "(\\s|$).*"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean containsWord( String keyword ) { | |
return (" " + description + " ").indexOf(" " + keyword + " ") >= 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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