Sprint View

2014 FRQ 1

5 min read

FRQ Question 1

This question involves reasoning about strings made up of uppercase letters. You will implement two related methods that appear in the same class (not shown). The first method takes a single string parameter and returns a scrambled version of that string. The second method takes a list of strings and modifies the list by scrambling each entry in the list. Any entry that cannot be scrambled is removed from the list.

FRQ Part A

Write the method scrambleWord, which takes a gievn word and returns a string that contains a scrambled version of the word according to the following rules

  • The scrambling process begin as the first letter of the word and continues from left to right.
  • If two consecutive letters consist of an “A” followed by a letter that is not an “A”, then the two letters are swapped in the resulting string.
  • Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap

The follwoing tbale show several example of word and their scrambled versions

word Result returned by scrambleWord(word)
“TAN” “TNA”
“ABRACADABRA” “BARCADABARA”
“WHOA” “WHOA”
“AARDVARK” “ARADVRAK”
“EGGS” “EGGS”
“A” “A”
”” ””


Complete method scrambleWord below.

Code Runner Challenge

Methods and Control Structures

View IPYNB Source
// CODE_RUNNER: Methods and Control Structures

// ScrambleTester class to test scrambleWord method
public class Main {

    /** Scrambles a given word.
     * @param word the word to be scrambled
     * @return the scrambled word (possibly equal to word)
     * Precondition: word is either an empty string or contains only uppercase letters.
     * Postcondition:
     * - Two consecutive letters consisting of "A" followed by a letter that was not "A" were swapped
     * - Letters were swapped at most once
     */
    private String scrambleWord(String word) {

        
        for (int i = 0; i < word.length() - 1; i++) {
            // Check if current character is 'A' and next character is not 'A'
            if (word.charAt(i) == 'A' && word.charAt(i + 1) != 'A') {
                // Swap the two characters
                String scrambled = word.substring(0, i) + word.charAt(i + 1) + word.charAt(i) + word.substring(i + 2);
                return scrambled;
            }
        }
        
        // If no "A" followed by non-"A" found, return original word
        return word;

    }

    public void driver() {

        String[] testWords = {
            "TAN",
            "ABRACADABRA",
            "AARDVARK",
            "AAAA",
            "",
            "BANANA"
        };

        for (String word : testWords) {
            System.out.println("Original word: " + word);
            String result = scrambleWord(word);
            System.out.println("Scrambled word: " + result);
            System.out.println("-----");
        }
    }

    public static void main(String[] args) {
        Main tester = new Main();
        tester.driver();
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

FRQ Part B


(b) Write the method scrambleOrRemove

Write the method scrambleOrRemove, which replaces each word in the parameter wordList with its scrambled version and removes any words that are unchanged after scrambling. The relative ordering of the entries in wordList remains the same as before the call to scrambleOrRemove.

Example

The following example shows how the contents of wordList would be modified as a result of calling scrambleOrRemove.

Before the call to scrambleOrRemove:

Index 0 1 2 3 4
wordList “TAN” “ABRACADABRA” “WHOA” “APPLE” “EGGS”

After the call to scrambleOrRemove:

Index 0 1 2
wordList “TNA” “BARCADABARA” “PAPLE”

Method Implementation

Assume that scrambleWord is in the same class as scrambleOrRemove and works as specified, regardless of what you wrote in part (a).

Complete method scrambleOrRemove below.

Code Runner Challenge

Methods and Control Structures

View IPYNB Source
// CODE_RUNNER: Methods and Control Structures
import java.util.List;
// ScrambleOrRemoveTester class to test scrambleOrRemove method
public class Main {

    /**
     * Modifies wordList by replacing each word with its scrambled
     * version, removing any words that are unchanged as a result of scrambling.
     * @param wordList the list of words
     * Precondition: wordList contains only non-null objects
     * Postcondition:
     *   - all words unchanged by scrambling have been removed from wordList
     *   - each of the remaining words has been replaced by its scrambled version
     *   - the relative ordering of the entries in wordList is the same as it was
     *     before the method was called
     */
    public static void scrambleOrRemove(List<String> wordList) {

        
        for (int i = wordList.size() - 1; i >= 0; i--) {
            String original = wordList.get(i);
            String scrambled = scrambleWord(original);
            
            // If the word didn't change after scrambling, remove it
            if (original.equals(scrambled)) {
                wordList.remove(i);
            } else {
                // Otherwise, replace it with the scrambled version
                wordList.set(i, scrambled);
            }
        }

    }

    // Provided helper method (assumed correct)
    private static String scrambleWord(String word) {
        // implementation not shown
        for (int i = 0; i < word.length() - 1; i++) {
            if (word.charAt(i) == 'A' && word.charAt(i + 1) != 'A') {
                String scrambled = word.substring(0, i) + word.charAt(i + 1) + word.charAt(i) + word.substring(i + 2);
                return scrambled;
            }
        }
        return word;
    }

    public void driver() {

        List<String> words = new java.util.ArrayList<>();
        words.add("TAN");
        words.add("ABRACADABRA");
        words.add("AAAA");
        words.add("BANANA");

        System.out.println("Original list: " + words);
        scrambleOrRemove(words);
        System.out.println("Modified list: " + words);
    }

    public static void main(String[] args) {
        Main tester = new Main();
        tester.driver();
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Scoring Guidelines Part A: scrambleWord — 5 points

Intent: Scramble a word by swapping all letter pairs that begin with A

Points Criterion
+1 Accesses all letters in word, left to right (no bounds errors)
+1 Identifies at least one letter pair consisting of “A” followed by non-“A”
+1 Reverses identified pair in constructing result string
+1 Constructs correct result string (Point lost if any letters swapped more than once, minor loop bounds errors ok)
+1 Returns constructed string

Scoring Guidelines Part (b): scrambleOrRemove — 4 points

Intent: Modify list by replacing each word with scrambled version and removing any word unchanged by scrambling

Points Criterion
+1 Accesses all words in wordList (no bounds errors)
+1 Calls scrambleWord with a word from the list as parameter
+1 Identifies words unchanged by scrambling
+1 On exit: List includes all and only words that have been changed by scrambling once, in their original relative order (minor loop bounds errors ok)

Course Timeline