2015 FRQ
import java.util.Arrays;
public class DiverseArray {
public static int arraySum(int[] arr) {
// add all the values in the array
int sum = 0;
for (int n : arr) {
sum += n;
}
return sum;
}
public static int[] rowSums(int[][] arr2D) {
// init array to store sums
int[] sums = new int[arr2D.length];
// iterate through the rows and use arraySum
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
public static boolean isDiverse(int[][] arr2D) {
// use rowSums and check for duplicate values
for (int i = 0; i < arr2D.length; i++) {
for (int j = i + 1; j < arr2D.length; j++) {
if (rowSums(arr2D)[i] == rowSums(arr2D)[j]) {
return false;
}
}
}
return true;
}
public static void main() {
// test part a
System.out.println(arraySum(new int[]{1, 3, 2, 7, 3}));
int[][] mat1 = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 6, 4, 2, 1}
};
int[][] mat2 = {
{1, 1, 5, 3, 4},
{12, 7, 6, 1, 9},
{8, 11, 10, 2, 5},
{3, 2, 3, 0, 6}
};
// test part b
System.out.println(Arrays.toString(rowSums(mat1)));
// test part c
System.out.println(isDiverse(mat1));
System.out.println(isDiverse(mat2));
}
}
DiverseArray.main();
16
[16, 32, 28, 20]
true
false
public class HiddenWord {
// instance variable for word to be guessed
private String word;
// constructor to set hidden word
public HiddenWord(String word) {
this.word = word;
}
public String getHint(String guess) {
// using StringBuilder to append ans
StringBuilder ans = new StringBuilder();
// iterate through letters of guess
for (int i = 0; i < guess.length(); i++) {
// if the letter is correct, append the letter
if (guess.charAt(i) == word.charAt(i)) {
ans.append(guess.charAt(i));
}
// if the letter is in the word, append '+'
else if (word.indexOf(guess.charAt(i)) != -1) {
ans.append('+');
}
// else, append '*'
else {
ans.append('*');
}
}
// return ans as a String
return ans.toString();
}
public static void main() {
// test
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("AAAAA"));
System.out.println(puzzle.getHint("HELLO"));
System.out.println(puzzle.getHint("HEART"));
System.out.println(puzzle.getHint("HARMS"));
System.out.println(puzzle.getHint("HARPS"));
}
}
HiddenWord.main();
+A+++
H****
H*++*
HAR*S
HARPS
// written by College Board
public class SparseArrayEntry {
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;
}
}
import java.util.ArrayList;
import java.util.List;
public class SparseArray {
private int numRows;
private int numCols;
private List<SparseArrayEntry> entries;
public SparseArray(int numRows, int numCols, ArrayList<SparseArrayEntry> entries) {
this.numRows = numRows;
this.numCols = numCols;
this.entries = entries;
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
public int getValueAt(int row, int col) {
for (SparseArrayEntry entry : entries) {
if (entry.getRow() == row && entry.getCol() == col) {
return entry.getValue();
}
}
return 0;
}
public void removeColumn(int col) {
numCols--;
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getCol() == col) {
entries.remove(i);
i--;
}
else if(entries.get(i).getCol() > col) {
entries.set(i, new SparseArrayEntry(entries.get(i).getRow(), entries.get(i).getCol() - 1, entries.get(i).getValue()));
}
}
}
public static void main() {
ArrayList<SparseArrayEntry> entries = new ArrayList<>();
entries.add(new SparseArrayEntry(1, 4, 4));
entries.add(new SparseArrayEntry(2, 0, 1));
entries.add(new SparseArrayEntry(3, 1, -9));
entries.add(new SparseArrayEntry(1, 1, 5));
SparseArray s = new SparseArray(6, 5, entries);
// test part a
System.out.println(s.getValueAt(3, 1));
System.out.println(s.getValueAt(3, 3));
}
}
SparseArray.main();
-9
0
public interface NumberGroup {
// interface to init contains
public boolean contains(int num);
}
public class Range implements NumberGroup {
int min;
int max;
// constructor to set min and max
public Range(int min, int max) {
this.min = min;
this.max = max;
}
// implement contains
@Override
public boolean contains(int num) {
return num >= min && num <= max;
}
}
import java.util.ArrayList;
import java.util.List;
public class MultipleGroups implements NumberGroup {
// instance variable for list of groupList
private List<NumberGroup> groupList;
public MultipleGroups(List<NumberGroup> groupList) {
this.groupList = groupList;
}
@Override
public boolean contains(int num) {
// check each entry for contains
for (NumberGroup entry : groupList) {
if (entry.contains(num)) {
return true;
}
}
return false;
}
public static void main() {
List<NumberGroup> entries = new ArrayList<>();
entries.add(new Range(5, 8));
entries.add(new Range(10, 12));
entries.add(new Range(1,6));
NumberGroup multiple1 = new MultipleGroups(entries);
// test part c
System.out.println(multiple1.contains(2));
System.out.println(multiple1.contains(9));
System.out.println(multiple1.contains(6));
}
}
MultipleGroups.main();
true
false
true