The examples below are from Traversing arrays and lists. Each has been translated into the AP CSP pseudocode language.
A list in the AP CSP pseudocode language is similar to a Java ArrayList
. It is possible to insert an element between other elements, to append an element to the end of a list, and to remove an element from a list. The syntax for accessing an element is similar to a Java array (square brackets). List indexes start at 1 and include the length of the list.
Loops in the AP CSP language are different than loops in Java. Where relevant, examples are shown in the AP CSP language using different approaches.
Sum of all elements in array
public static int getSum(int[] nums)
{
int sum = 0;
for(int i = 0; i < nums.length; i++)
{
sum = sum + nums[i];
}
return sum;
}
Using AP CSP REPEAT n TIMES
loop
PROCEDURE getSum(nums)
{
sum ← 0
index ← 1
REPEAT LENGTH (nums) TIMES
{
sum ← sum + nums[index]
index ← index + 1
}
RETURN (sum)
}
The loop does not change the length of nums
, so this is unambiguous. It is unlikely that this loop type would be used on the AP CSP Exam for a problem in which the loop body changed the length of the list. The loop behavior in such a situation is not specified.
Using AP CSP REPEAT UNTIL
loop
PROCEDURE getSum(nums)
{
sum ← 0
index ← 1
REPEAT UNTIL (index > LENGTH (nums))
{
sum ← sum + nums[index]
index ← index + 1
}
RETURN (sum)
}
Sum using an enhanced for
loop
public static int getSum(int[] nums)
{
int sum = 0;
for(int n : nums)
{
sum = sum + n;
}
return sum;
}
Using AP CSP FOR EACH
loop
PROCEDURE getSum(nums)
{
sum ← 0
FOR EACH element IN nums
{
sum ← sum + element
}
RETURN (sum)
}
The AP CSP FOR EACH
loop works well in this situation. It avoids the use of an index. The behavior is unambiguous in this situation.
Average of range in list
public static double getAverageInRange(
ArrayList<Double> nums,
int start, int end)
{
double sum = 0;
int count = 0;
for(int i = start; i <= end; i++)
{
sum += nums.get(i);
count++;
}
return sum / count;
}
Using AP CSP REPEAT UNTIL
loop
PROCEDURE getAverageInRange(nums, start, end)
{
sum ← 0
count ← 0
index ← start
REPEAT UNTIL (index > end)
{
sum ← sum + nums[index]
count ← count + 1
index ← index + 1
}
RETURN (sum / count)
}
As discussed above, the AP CSP language does not distinguish between an array and a list. The behavior matches a Java ArrayList
. The element access matches a Java array.
Count Evans
public static int countEvans(String[] names)
{
int evans = 0;
for(String n : names)
{
if(n.equals("Evan"))
evans++;
}
return evans;
}
Using AP CSP FOR EACH
loop
PROCEDURE countEvans(names)
{
evans ← 0
FOR EACH n IN names
{
IF(n = "Evan")
{
evans ← evans + 1
}
}
RETURN (evans)
}
The AP CSP language uses =
to check for equality, regardless of type.
Filter evens
public static ArrayList<Integer> filterEvens(ArrayList<Integer> nums)
{
ArrayList<Integer> evens = new ArrayList<Integer>();
for(Integer n : nums)
{
if(n % 2 == 0)
evens.add(n);
}
return evens;
}
Using AP CSP FOR EACH
loop
PROCEDURE filterEvens(nums)
{
evens ← []
FOR EACH n IN nums
{
IF((n MOD 2) = 0)
{
APPEND(evens, n)
}
}
RETURN (evens)
}
Filter palindromes
public static boolean isPalindrome(String str)
{
if(str.length() <= 1)
return true;
return str.substring(0, 1).equals(str.substring(str.length() - 1)) &&
isPalindrome(str.substring(1, str.length() - 1));
}
public static ArrayList<String> getPalindromes(String[] words)
{
ArrayList<String> palindromes = new ArrayList<String>();
for(String w : words)
{
if(isPalindrome(w))
palindromes.add(w);
}
return palindromes;
}
Using AP CSP FOR EACH
loop
Procedure isPalindrome
returns true
if string str
is a palindrome, false
otherwise.
PROCEDURE isPalindrome(str)
The code for procedure isPalindrome
is not shown. Obtaining parts of a string is not defined in the AP CSP language. Specific problems on the AP CSP Exam may define methods similar to the Java substring
methods.
PROCEDURE getPalindromes(words)
{
palindromes ← []
FOR EACH w IN words
{
IF(isPalindrome(w))
{
APPEND(palindromes, w)
}
}
RETURN (palindromes)
}
Make zero above threshold
public static void makeZero(ArrayList<Integer> nums, int threshold)
{
for(int i = 0; i < nums.size(); i++)
{
if(nums.get(i) > threshold)
{
nums.set(i, 0);
}
}
}
Using AP CSP REPEAT UNTIL
loop
PROCEDURE makeZero(nums, threshold)
{
index ← 1
REPEAT UNTIL (index > LENGTH (nums))
{
IF(nums[index] > threshold)
{
nums[index] ← 0
}
index ← index + 1
}
}
Help & comments
Get help from AP CS Tutor Brandon Horn