Other 2014 FR Questions FR other years Be Prepared Home
A-1
Part (a)
  public static String scrambleWord(String word)
  {
    String scrambled = "";
    int i = 0;
    
    while (i < word.length())
    {
      String letter1 = word.substring(i, i+1);
      String letter2 = "";
      if (i < word.length() - 1)
        letter2 = word.substring(i+1, i+2);

      if (letter1.equals("A") && !letter2.equals("A") && !letter2.equals(""))
      {
        scrambled += letter2 + letter1;
        i += 2;
      }
      else
      {
        scrambled += letter1;
        i += 1;
      }
    }
    return scrambled;
  }

Part (b)
  public static void scrambleOrRemove(List<String> wordList)
  {
    int i = 0;
    while (i < wordList.size())
    {
      String word = wordList.get(i);
      String scrambled = scrambleWord(word);
      if (!scrambled.equals(word))
      {
        wordList.set(i, scrambled);
        i++;
      }
      else
        wordList.remove(i);
    }
  } 1
Notes:
  1. Alternative solution:
      public static void scrambleOrRemove(List<String> wordList)
      {
        for (int i = wordList.size() - 1; i >= 0; i--)
        {
          String word = wordList.get(i);
          String scrambled = scrambleWord(word);
          if (!scrambled.equals(word))
            wordList.set(i, scrambled);
          else
            wordList.remove(i);
        }
      }

Other 2014 FR Questions | Back to Contents

Copyright © 2014 by Skylight Publishing
support@skylit.com