Created
May 11, 2015 01:20
-
-
Save Anon10W1z/57a8fb48beac9ba7168e to your computer and use it in GitHub Desktop.
AP Comp Sci 2015 FRQ Answers
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 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; | |
} | |
} |
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
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; | |
} | |
} |
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
package numbergroup; | |
public interface NumberGroup { //part a | |
boolean contains(int i); | |
} |
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
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; | |
} | |
} |
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
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; | |
} | |
} |
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
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