////// problem 1
///// 1 A
public static String encrypt(String text, int key)
{
String str = "";
String change = "";
int index = 0;
for(int i = 0; i < key.length(); i++)
{
str = text.substring(i, i + 1);
index = abc.indexOf(str);
if(index != -1)
{
str = abc.substring(((index + key) % 26));
}
change+=str;
}
return change;
}
///// 1 B
public static String decrypt(String code, int key)
{
return encrypt(code, 26 - key);
}
//// Problem 2
//// 2 A
public class APStudent
{
private int numPassingTests;
private int countTests;
private int totalSumScores;
public void addExam(int grade)
{
countTests++;
totalSumScores+=grade;
if(grade >= 3)
{
numPassingTests++;
}
} Try to use constant variables
public int awardLevel()
{
double average = (double)(totalSumScores) / countTests;
if(numPassingTests > 4 && average >= 3.5)
{
return 3;
}
if(numPassingTests > 3 && average >= 3.25)
{
return 2;
}
if(numPassingTests > 2)
{
return 1;
}
return 0;
}
}
//// Problem 3
//// 3 A
public static ArrayList<Integer> findRPeaks(double[] v)
{
ArrayList<Integer> rPeaks = new ArrayList<Integer>();
for(int k = DELTA; k <= v.length - 1 - DELTA; k++)
{
if(isRPeak(v, k))
{
rPeaks.add(k);
k+=DELTA - 1;
}
}
return rPeaks;
}
///// 3 B
public static int heartRate(ArrayList<Integer> rPeakPositions)
{
double averageRate = (rPeakPositions.get(0) +
rPeakPositions.get(rPeakPositions.size() - 1)) / Confusing and there are n-1
(double)(rPeakPositions.size()); intervals, so it has
to be
rPeakPositions.size() - 1
return (int)(60 * (SAMPLING_SIZE / averageRate));
} Round by adding 0.5
///// Problem 4
//// 4 A
public static int vacantGoodSeats(boolean[][] seats, int w, int h)
{
int startW = (seats[0].length - w) / 2;
int counter = 0;
for(int i = 2; i <= h + 1; i++)
{
for(int j = startW; j < startW + w; j++)
{
if(!seats[i][j])
{
counter++;
}
}
}
return counter;
}
///// 4 B
public static Location bestTwoSeats(boolean[][] seats)
{
Location best = null;
double distance = 1000.0;
double temp = 0.0; (seats.length - 1) /
int centerLeftW = seats[0].length / 2 - 1; 2
int centerLeftH = seats.length / 2 - 1 + seats.length % 2;
Location center = new Location(centerLeftH, centerLeftW);
for(int h = 0; h < seats.length; h++)
{
for(int w = 0; w < seats[0].length; w+=2)
{
if(!seats[h][w] && !seats[h][w+1])
{
temp = center.distanceFrom(new Location(h, w));
if(temp < distance)
{
best = new Location(h, w);
distance = temp;
}
}
} Can just use 2 Location Objects, one
}
return best;
for the center and one for the best
} distance and find the closest space.
Too complicated