Last active
May 23, 2021 12:51
-
-
Save gschaffner/0c1b674687b261200e6a5c1a099afcf4 to your computer and use it in GitHub Desktop.
APCS Written Test Practice Barron's 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
// 1a | |
public static void reverseArray(int[] arr) { | |
for(int i = 0; i = arr.length / 2; i++) { | |
arr[i] = arr[i] + arr[arr.length - i - 1]; | |
arr[arr.length - i - 1] = arr[i] - arr[arr.length - i - 1]; | |
arr[i] = arr[i] - arr[arr.length - i - 1]; | |
} | |
} | |
// 1b | |
public void reverseAllRows() { | |
for(int[] row : mat) | |
ArrayUtil.reverseArray(row); | |
} | |
// 1c | |
public void reverseMatrix() { | |
reverseAllRows(); | |
for(int i = 0; i < mat.length / 2; i++) { | |
int[] temp = mat[i]; | |
mat[i] = mat[mat.length - i - 1]; | |
mat[mat.length - i - 1] = temp; | |
} | |
} | |
// 2a | |
public List<Integer> getBlankPositions() { | |
List<Integer> toReturn = new List<Integer>(); | |
String temp = sentence; | |
while(temp.indexOf(' ') != -1) { | |
toReturn.add(temp.indexOf(' '); | |
temp = temp.substring(temp.indexOf(' ')); | |
} | |
return toReturn; | |
} | |
// 2b | |
public int countWords() { | |
if(sentence.length() != 0) | |
return (getBlankPositions().size() + 1); | |
return 0; | |
} | |
// 2c | |
public String[] getWords() { | |
List<Integer> blankPos = getBlankPositions(); | |
blankPos.add(sentence.length() + 1); | |
String[] words = new String[countWords()]; | |
for(int i = 0; i < countWords(); i++) { | |
words[i] = sentence.substring(blankPos.get(i) + 1, blankPos.get(i + 1)); | |
} | |
return words; | |
} | |
// 3a | |
public Player requestSlot(String playerName) { | |
for(int i = 0; i < slots.length; i++) { | |
if(slots[i] == null) { | |
slots[i] = new Player(playerName, i); | |
return slots[i]; | |
} | |
} | |
waitingList.add(playerName); | |
return null; | |
} | |
// 3b | |
public Player cancelAndReassignSlot(Player p) { | |
slots[p.getPlayerNumber()] = null; | |
if(waitingList.size() != 0) { | |
slots[p.getPlayerNumber()] = new Player(waitingList.remove(0), p.getPlayerNumber()); | |
} | |
return slots[p.getPlayerNumber()]; | |
} | |
// 4a | |
public void reset() { | |
if(arm.isFacingRight()) | |
arm.changeDirection(); | |
while(arm.getCurrentIndex() != 0) | |
arm.moveForward(1); | |
// answer key has while loop replaced with arm.moveForward(arm.getCurrentIndex()) | |
arm.changeDirection(); | |
} | |
// 4b | |
public int mostAcidic() { | |
reset(); | |
int minPH = 7, minIndex = 0; | |
for(int i = 0; i < solutions.size(); i++) { | |
Solution s = solutions.get(i); | |
if(s.getPH() < minPH) { | |
minIndex = i; | |
minPH = s.getPH(); | |
} | |
} | |
if(minPH == 7) { | |
return -1; | |
} else { | |
arm.moveForward(minIndex); | |
return minIndex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can we have 4c please