Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queues - Ting Wong - Whiteboard Practice 2 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


## 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍