diff --git a/README.md b/README.md index 632a728..460a329 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,34 @@ then it should return the following values after the call: [1, 3, 2, 7, 7, 2, 3, 1] Notice that it has been doubled in size by having the original sequence appearing in reverse order at the end of the list. You may not make assumptions about how many elements are in the list. + def mirror(array) + counter = array.length - 1 + new_array = [] + array.length.times do + new_array << array[counter] + counter -= 1 + end + + result = array + new_array + end + ## Problem #2 Write a method `switchPairs` that switches the order of values in an Array of strings in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. **This switch should happen _in place_, meaning no new array should be created.** For example, if the list initially stores these values: ["four", "score", "and", "seven", "years", "ago"] your method should switch the first pair, "four", "score", the second pair, "and", "seven", and the third pair, "years", "ago", to yield this list: ["score", "four", "seven", "and", "ago", "years"] If there are an odd number of values in the list, the final element is not moved. For example, if the original list had been: ["to", "be", "or", "not", "to", "be", "hamlet"] It would again switch pairs of values, but the final value, "hamlet" would not be moved, yielding this list: ["be", "to", "not", "or", "be", "to", "hamlet"] + + def switchPairs(array) + ind1 = 0 + ind2 = 1 + + t = array.length / 2 + + t.times do + first_value = array[ind1] + array[ind1] = array[ind2] + array[ind2] = first_value + ind1 += 2 + ind2 += 2 + end + return array + end