Transformations

There are three transformations that may be applied to the puzzle.  These transformations are used by the program to scramble the puzzle in the beginning, and then they are used by the person playing the game in order to attempt to solve the puzzle.  You will implement these three transformations in this project:

swap(i, j):
Given two positions i and j, this transformation swaps the characters at these positions. An example is shown in the figure below.

The transformation swap(i, i) is allowed, but has no effect on the word.  You may not assume that the first argument is smaller than the second argument.

insert(i, j):
Given two positions i and j, this transformation moves the character at position i to position j. All the characters in between are then shifted by one position, either to the right or left, to make room for the moved character. This is illustrated in the following figure.

The transformation insert(i, i) is allowed, but has no effect on the word.  You may not assume that the first argument is smaller than the second argument.

multipleSwap(i, j):
Given two positions i and j, this transformation swaps pairs of characters between i and j, from the outermost pair to the innermost pair. That is, in the case where i < j:  the character at index i will be swapped with the character at index j, the character at index i+1 will be swapped with the character at index j-1, the character at index i+2 will be swapped with the character at index j-2 and so on and so forth. An example is shown in the figure below.

Note that you must consider two cases: (1) the number of characters between i and j is even; (2) the number of characters between i and j is odd. In the latter case, the middle-most character remains in place.  Note: You are not required to write your code so that multipleSwap calls swap.

The transformation multipleSwap(i, i) is allowed, but has no effect on the word.  You may not assume that the first argument is smaller than the second argument.

 

Web Accessibility