I have a method that swaps two items:
swap( collection['a'], collection['b'] ); // result = 'b', 'a'
swap( collection[0], collection[1] ); // result = collection[1], collection[0]
swap( 0, collection.indexOf(collection[1]) ); // result collection[1] index, 0
- The swap method cannot be modified.
- Four possible values stored in a collection: 'a', 'b', 'c', 'd'
- Swapped to always be in this order 'd', 'b', 'a', 'c'
- Any of the four possible values may or may not be in the collection
Please help me to implement this algorithm.
Thanks!
For those that care, this is not homework.
Examples:
//Example 1:
//collection contains: 'a', 'b', 'c', 'd'
//desired order: 'd', 'b', 'a', 'c'
swap(0, collection.IndexOf(collection['d']));
swap(1, collection.IndexOf(collection['b']));
swap(2, collection.IndexOf(collection['a']));
swap(3, collection.IndexOf(collection['c']));
//Example 2:
//collection contains: 'a', 'b', 'c'
//desired order: 'b', 'a', 'c'
swap(0, collection.IndexOf(collection['b']));
swap(1, collection.IndexOf(collection['a']));
swap(2, collection.IndexOf(collection['c']));