Skip to content

Instantly share code, notes, and snippets.

@Anon10W1z
Created May 11, 2015 01:20
Show Gist options
  • Save Anon10W1z/57a8fb48beac9ba7168e to your computer and use it in GitHub Desktop.
Save Anon10W1z/57a8fb48beac9ba7168e to your computer and use it in GitHub Desktop.
AP Comp Sci 2015 FRQ Answers
public class ArraySums { //question 1
public static int arraySum(int[] arr) { //part a
int arraySum = 0;
for (int i : arr)
arraySum += i;
return arraySum;
}
public static int[] rowSums(int[][] arr2D) { //part b
int[] rowSums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; ++i)
rowSums[i] = arraySum(arr2D[i]);
return rowSums;
}
public static boolean isDiverse(int[][] arr2D) { //part c
int[] rowSums = rowSums(arr2D);
for (int rowSum : rowSums)
for (int rowSum2 : rowSums)
if (rowSum == rowSum2)
return false;
return true;
}
}
public class HiddenWord { //question 2
private String word;
public HiddenWord(String word) {
this.word = word;
}
public String getHint(String guess) {
String hint = "";
for (int i = 0; i < word.length(); ++i)
if (word.charAt(i) == guess.charAt(i))
hint += word.charAt(i);
else if (word.contains("" + guess.charAt(i)))
hint += '+';
else hint += '*';
return hint;
}
}
package numbergroup;
import java.util.Arrays;
import java.util.List;
public class MultipleGroups implements NumberGroup { //part c
private List<NumberGroup> groupList;
public MultipleGroups(NumberGroup... groups) {
this.groupList = Arrays.asList(groups);
}
@Override
public boolean contains(int i) {
for (NumberGroup group : groupList)
if (group.contains(i))
return true;
return false;
}
}
package numbergroup;
public interface NumberGroup { //part a
boolean contains(int i);
}
package numbergroup;
public class Range implements NumberGroup { //part b
private int min;
private int max;
public Range(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public boolean contains(int i) {
return this.min <= i && i <= this.max;
}
}
package sparsearray;
import java.util.ArrayList;
import java.util.List;
public class SparseArray { //question 3
private int numRows;
private int numCols;
private List<SparseArrayEntry> entries;
public SparseArray() {
entries = new ArrayList<>();
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
public int getValueAt(int row, int col) { //part a
for (SparseArrayEntry entry : entries)
if (entry.getRow() == row && entry.getCol() == col)
return entry.getValue();
return 0;
}
public void removeColumn(int col) { //part b
for (int i = 0; i < entries.size(); ++i) {
SparseArrayEntry entry = entries.get(i);
if (entry.getCol() == col)
entries.remove(entry);
else if (entry.getCol() > col)
entries.set(i, new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue()));
}
--numCols;
}
}
package sparsearray;
public class SparseArrayEntry { //question 3
private int row;
private int col;
private int value;
public SparseArrayEntry(int r, int c, int v) {
row = r;
col = c;
value = v;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment