Scramble or remove Download

Christina Cacioppo

strings, for loops, functions, arraylists Student approved


Introduction

Thought we'd done enough scrambling last time, eh? No luck: now we're going to scramble an entire list of words!


Task

Write the scrambleOrRemove method. It takes an ArrayList, and your code should:

  1. Look through each entry in the ArrayList and scramble it using scrambleWord.
  2. If the original and scrambled words are identical, remove that entry from the List.
  3. Otherwise, move on to the next item in the list.


Starter code

class ScrambleOrRemove{

	/* Scrambles each word in the list and return
	 * another list of only those that are unchanged
	 * @ param 	wordList – the list of words to be scrambled
	 *     or removed
	 */
	ArrayList scrambleOrRemove(ArrayList wordList){
		// Start scrambling!
	}

	/* Returns a scrambled version of the word it's passed
	 * @ param 	word – the word to be scrambled
	 */
	String scrambleWord(String word){
		int current = 0;
		String result = "";

		while(current < word.length() - 1){
			if(word.substring(current, current + 1).equals("A") && !word.substring(current + 1, current + 2).equals("A")){
				result += word.substring(current +1, current + 2);
				result += "A";
				current += 2;
			}else{
				result += word.substring(current, current + 1);
				current++;
			}
		}

		if(current < word.length()){
			result += word.substring(current);
		}

		return result;
	}
}

Expected output

Output will depend on what list is passed, but, for example, if wordList started out like this:
["TAN","ABRACADABRA","WHOA","APPLE","EGGS"]
After calling `scrambleOrRemove(wordList)`, it should look like this:
["TNA","BARCADABARA","PAPLE"]

Useful tools to solve the problem