Phrase free response answer 9

Phrase free response problem from the 2017 AP Computer Science A Exam.

Phrase is #3 from the from the 2017 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/ap/pdf/ap-computer-science-a-frq-2017.pdf

Part (a) – replaceNthOccurrence method

public void replaceNthOccurrence(String str, int n, String repl)
{
  int index = findNthOccurrence(str, n);

  if(index != -1)
  {
    currentPhrase = currentPhrase.substring(0, index) + repl +
        currentPhrase.substring(index + str.length());
  }
}

Part (b) – findLastOccurrence method (considered solution)

This is my considered solution using the method from part (a). This solution has the advantage of not running findNthOccurrence an extra time. The solution I wrote under time constraints is farther down.

public int findLastOccurrence(String str)
{
  int n = 1;
  int index = -1;
  int nextIndex = findNthOccurrence(str, n);
  while(nextIndex != -1)
  {
    index = nextIndex;
    n++;
    nextIndex = findNthOccurrence(str, n);
  }

  return index;
}

Part (b) – findLastOccurrence method (solution outside Exam)

This solution uses the lastIndexOf method, which isn’t in the AP Java Subset. This is what I would write if I had to perform this task as part of an actual program.

To clarify, this solution is not acceptable on the Exam because the question requires the use of the method from part (a).

public int findLastOccurrence(String str)
{
  return currentPhrase.lastIndexOf(str);
}

Part (b) – findLastOccurrence method (initial solution)

This was my solution under time constraints. I advise my students to avoid looking for solutions outside the subset on the Exam, so I avoid them as well.

public int findLastOccurrence(String str)
{
  int n = 1;
  while(findNthOccurrence(str, n) != -1)
    n++;

  if(n == 1)
    return -1;

  return findNthOccurrence(str, n - 1);
}

2017 AP CS Exam Free Response Solutions

Please note: The College Board has not yet released scoring guidelines for the 2017 AP CS A FR (as of this writing). Questions of the form, “How many points would I lose for ___?” can’t be answered.

9 thoughts on “Phrase free response answer

  1. Reply Anonymous May 4,2017 7:12 pm

    Last index of isnt allowed

    • Reply Brandon Horn May 4,2017 7:27 pm

      True, but only because of the requirement that students use the method from part (a). Students are allowed to use any valid Java API methods, though I don’t encourage my students to look for solutions outside the subset during the Exam.

      I clarified the header for my lastIndexOf solution and added an explicit note that it isn’t a valid solution on the Exam.

  2. Reply John May 4,2017 9:07 pm

    I didn’t see that you had to use findNthOccurrence for findLastOccurrence. Instead I used a for loop going backwards. My program produces the right output but how much will I get docked off for not using findNthOccurrence.

    • Reply Brandon Horn May 4,2017 9:14 pm

      You’ll almost certainly lose something. The scoring guide hasn’t been released yet so I can’t say how much.

  3. Reply Anonymous May 4,2017 11:19 pm

    Do you not have to consider edge cases in part a? What if the part after repl has an out of bounds error?

    • Reply Brandon Horn May 5,2017 6:52 am

      This is a great question. In general you do have to consider edge cases unless they are covered by a precondition. The College Board hasn’t released the scoring guides yet so we don’t know for sure in this case. My guess would be yes, you have to avoid going out of bounds.

      My solution avoids this issue with careful use of both substring methods. You can ask for a substring of length 0 from the 2 parameter substring method and you can go 1 spot out of bounds (but not more) with the 1 parameter substring method.

      "brandon".substring(0, 0) will return an empty string.
      "brandon".substring(7) will return an empty string.

      "brandon".substring(7, 8) will cause a runtime error.

  4. Reply Anonymous May 5,2017 1:19 pm

    What about searching for the string starting from the end of the phrase on findLastOccurrence?

    • Reply Brandon Horn May 6,2017 10:21 am

      The instructions specified that you must use the method from part (a). A solution that didn’t would almost certainly not receive full credit.

  5. Reply Mike Bagg May 24,2017 2:13 pm

    In case you want to run the methods of phase to see output, here is findNthOccurrence:

    public int findNthOccurrence(String str, int n)
    {
      int count = 0;
      int len = str.length();
      int buffer = len-1;
      int index=0;
    
      for(int i = 0; i < currentPhrase.length()-buffer; i++)
      {
        if(currentPhrase.substring(i,len).equals(str))
        {
          index = i;
          count++;
    
          if(count == n)
            return index;
        }    			
    
        len++;
      }    	
    
      if(count != n)
        return -1;
      else
        return index;
        	
    }
    

    Note: formatting adjusted

Leave a Reply