FRQ 2011 Post
FRQ 2011 blog post for AP CSA
// Part a
public static int arraySum(int[] arr) {
int sum = 0;
for (int element : arr) {
sum += element;
}
return sum;
}
// Part b
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
// Part c
public static boolean isDiverse(int[][] arr2D) {
for (int i = 0; i < rowSums[arr2D]; i++) {
for (int j = i + 1; j < rowSums[arr2D]; j++) {
if (rowSums[arr2D][j] == rowSums[arr2D][i]) {
return false;
}
}
}
return true;
}
public class HiddenWord {
private String hiddenWord;
public HiddenWord(String word) {
hiddenWord = word;
}
public static String getHint(String guess) {
String hint = "";
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == hiddenWord.charAt(i)) {
hint += guess.charAt(i);
} else if (word.contains(guess.charAt(i))) {
hint += "+";
} else {
hint += "*";
}
}
return hint;
}
}
// Part a
public int getValueAt(int row, int col) {
int value = 0;
for (SparseArrayEntry item : entries) {
if (item.getRow() == row && item.getCol() == col) {
value = item.getValue();
break;
}
}
return value;
}
// Part b
public void removeColumn(int col) {
for (SpaceArrayEntry item : entries) {
if (item.getCol() == col) {
entries.remove(item);
} else if (item.getCol() > col) {
item.getCol()--;
}
}
}
// Part a
public interface NumberGroup {
public boolean contains(int num);
}
// Part b
public class Range implements NumberGroup {
private int[] nums;
public Range(int min, int max) {
nums = new int[max - min];
int i = min;
int index = 0;
while (i <= max) {
nums[index] = i;
index++;
i++;
}
}
public int[] getNums() {
return nums;
}
}
// Part c
public boolean contains(int num) {
for (Range range : groupList) {
if (nums >= range.getNums()[0] && nums <= range.getNums()[range.getNums().length - 1]) {
return true;
}
}
return false;
}
// Part a
public boolean simulate() {
int travel = 0;
for (int i = 0; i < maxHops; i++) {
travel += hopDistance();
if (travel >= goalDistance) {
return true;
} else if (travel < 0) {
return false;
}
}
return false;
}
// Part b
public double runSimulations(int num) {
double proportion = 0;
for (int i = 0; i < num; i++) {
if (simulate() == true) {
proportion++;
}
}
proportion /= num;
return proportion;
}
// Part a
public WordPairList(String[] words) {
allPairs = new ArrayList<WordPair>();
for (int i = 0; i < words.length - 1; i++) {
for (int j = i + 1; j < words.length; j++) {
WordPair pair = new WordPair(words[i], words[j]);
allPairs.add(pair);
}
}
}
// Part b
public int numMatches() {
int matches = 0;
for (WordPair pair : allPairs) {
if (pair.getFirst().equals(pair.getSecond())) {
matches++;
}
}
return matches;
}
// Part a
public static int[] getColumn(int[][] arr2D, int c) {
int[] column = new int[arr2D.length];
int index = 0;
for (int[] row : arr2D) {
column[index] = row[c];
index++;
}
return column;
}
// Part b
public static boolean isLatin(int[][] square) {
if (containsDuplicates(square[0]) == true) {
return false;
}
for (int i = 1; i < square.length; i++) {
if (hasAllValues(square[0], square[i]) == false) {
return false;
}
}
for (int j = 0; j < square[0].length; j++) {
if (hasAllValues(square[0], getColumn(square, j)) == false) {
return false;
}
}
return true;
}
public static int firstAvailableBlock(int period, int duration) {
int minutes = 0;
for (int i = 0; i <= 59; i++) {
if (freeMinute(period, i) == true) {
minutes++;
} else {
minutes = 0;
}
if (minutes == duration) {
return i - duration + 1;
}
}
return -1;
}