2014 FRQ
import java.util.Arrays;
public class WordScrambler {
private String[] scrambledWords;
public WordScrambler(String[] wordArr) {
scrambledWords = mixedWords(wordArr);
}
private String recombine(String word1, String word2) {
// combines the first half of the first word and the second half of the second word using substring()
return (word1.substring(0, word1.length() / 2) + word2.substring(word2.length() / 2));
}
private String[] mixedWords(String[] words) {
// init new arr to store recombined words
String[] arr = new String[words.length];
// iterate through words and store new recombined words in arr in pairs
for (int i = 0; i < words.length / 2; i++) {
arr[i * 2] = recombine(words[i * 2], words[i * 2 + 1]);
arr[i * 2 + 1] = recombine(words[i * 2 + 1], words[i * 2]);
}
return arr;
}
public static void main() {
// test part a
WordScrambler w = new WordScrambler(new String[]{"apple", "pear", "this", "cat"});
System.out.println(w.recombine("apple", "pear"));
System.out.println(w.recombine("pear", "apple"));
// test part b
System.out.println(Arrays.toString(w.mixedWords(new String[]{"apple", "pear", "this", "cat"})));
}
}
WordScrambler.main();
apar
peple
[apar, peple, that, cis]
public class Mountain {
public static boolean isIncreasing(int[] array, int stop) {
// if one term is greater than the succeeding term, return false
for (int i = 0; i < stop; i++) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
public static boolean isDecreasing(int[] array, int start) {
// if one term is less than the succeeding term, return false
for (int i = start; i < array.length - 1; i++) {
if (array[i] < array[i + 1]) {
return false;
}
}
return true;
}
public static int getPeakIndex(int[] array) {
// return the index if the preceding and succeeding value are both smaller
for (int i = 1; i < array.length - 1; i++)
if (array[i - 1] < array[i] && array[i] > array[i + 1]) {
return i;
}
// otherwise return -1
return -1;
}
public static boolean isMountain(int[] array) {
// if a peak exists
if (getPeakIndex(array) != -1) {
// check if the mountain is increasing to the peak and decreasing after the peak
return isIncreasing(array, getPeakIndex(array)) && isDecreasing(array, getPeakIndex(array));
}
return false;
}
public static void main() {
// test part a
System.out.println(getPeakIndex(new int[]{11, 22, 33, 22, 11}));
System.out.println(getPeakIndex(new int[]{11, 22, 11, 22, 11}));
System.out.println(getPeakIndex(new int[]{11, 22, 33, 55, 77}));
System.out.println(getPeakIndex(new int[]{99, 33, 55, 77, 120}));
System.out.println(getPeakIndex(new int[]{99, 33, 55, 77, 55}));
System.out.println(getPeakIndex(new int[]{33, 22, 11}));
// test part b
System.out.println(isMountain(new int[]{1, 2, 3, 2, 1}));
System.out.println(isMountain(new int[]{1, 2, 1, 2, 1}));
System.out.println(isMountain(new int[]{1, 2, 3, 1, 5}));
System.out.println(isMountain(new int[]{1, 4, 2, 1, 0}));
System.out.println(isMountain(new int[]{9, 3, 5, 7, 5}));
System.out.println(isMountain(new int[]{3, 2, 1}));
}
}
Mountain.main();
2
1
-1
-1
3
-1
true
false
false
true
false
false
public class TemperatureGrid {
private double[][] temps;
// default constructor to set temps
public TemperatureGrid(double[][] temps) {
this.temps = temps;
}
private double computeTemp(int row, int col) {
// if on the edge of temps, return original temp
if (row == 0 || row == temps.length - 1 || col == 0 || col == temps[0].length - 1) {
return temps[row][col];
}
// otherwise, return the average of all adjacent values
return (temps[row - 1][col] + temps[row + 1][col] + temps[row][col - 1] + temps[row][col + 1]) / 4;
}
public boolean updateAllTemps(double tolerance) {
// loop through temps
for (int i = 0; i < temps.length; i++) {
for (int j = 0; j < temps[0].length; j++) {
// if the difference between the value and value after computeTemp is greater than tolerance, return false
if (Math.abs(temps[i][j] - computeTemp(i, j)) > tolerance) {
return false;
}
}
}
return true;
}
public static void main() {
// example temps
double[][] temps = {
{95.5, 100, 100, 100, 100, 110.3},
{0, 50, 50, 50, 50, 0},
{0, 40, 40, 40, 40, 0},
{0, 20, 20, 20, 20, 0},
{0, 0, 0, 0, 0, 0}
};
TemperatureGrid t = new TemperatureGrid(temps);
// test part a
System.out.println(t.computeTemp(2, 3));
System.out.println(t.computeTemp(1, 1));
System.out.println(t.computeTemp(0, 2));
System.out.println(t.computeTemp(1, 3));
// test part b
System.out.println(t.updateAllTemps(0.01));
}
}
TemperatureGrid.main();
37.5
47.5
100.0
60.0
false
// copied from College Board
public class ScoreInfo {
private int score;
private int numStudents;
public ScoreInfo(int aScore) {
score = aScore;
numStudents = 1;
}
public void increment() {
numStudents++;
}
public int getScore() {
return score;
}
public int getNumStudents() {
return numStudents;
}
}
import java.util.ArrayList;
public class Stats {
private ArrayList<ScoreInfo> scoreList;
public boolean record(int score) {
// loop through scoreList
for (int i = 0; i < scoreList.size(); i++) {
// if score already exists, increment score and return false
if (scoreList.get(i).getScore() == score) {
scoreList.get(i).increment();
return false;
}
// otherwise, when reaching a score higher than the score, insert score and return true
else if (scoreList.get(i).getScore() > score) {
scoreList.add(i, new ScoreInfo(score));
return true;
}
}
return false;
}
public void recordScores(int[] stuScores) {
// loop through stuScores and record all scores
for (int score : stuScores) {
record(score);
}
}
}