Last active
June 14, 2017 10:26
-
-
Save benvds/98df2e4f0ee2076d905d9cba5f8f3c2a to your computer and use it in GitHub Desktop.
Move an array item to specified index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrayOrdering | |
# Returns clone of the array with subject moved into the new idx | |
def self.move_to_position(_list, subject, new_idx) | |
old_idx = _list.index(subject) | |
return _list unless old_idx != new_idx | |
list = _list.clone | |
if old_idx.nil? | |
list.insert(new_idx, subject) | |
else | |
list.insert(new_idx, list.delete_at(old_idx)) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require_relative './array_ordering' | |
class ArrayOrderingTest < Minitest::Test | |
describe ArrayOrdering do | |
let(:first) {1 } | |
let(:second) { 2 } | |
let(:third) { 3 } | |
let(:fourth) { 4 } | |
let(:list) { [first, second, third, fourth] } | |
it 'swaps with first position' do | |
sorted = ArrayOrdering.move_to_position(list, second, 0) | |
assert sorted.eql? [second, first, third, fourth] | |
end | |
it 'moves to first position' do | |
sorted = ArrayOrdering.move_to_position(list, third, 0) | |
assert sorted.eql? [third, first, second, fourth] | |
end | |
it 'swaps lower' do | |
sorted = ArrayOrdering.move_to_position(list, third, 1) | |
assert sorted.eql? [first, third, second, fourth] | |
end | |
it 'moves lower' do | |
sorted = ArrayOrdering.move_to_position(list, fourth, 1) | |
assert sorted.eql? [first, fourth, second, third] | |
end | |
it 'leaves in place' do | |
sorted = ArrayOrdering.move_to_position(list, third, 2) | |
assert sorted.eql? [first, second, third, fourth] | |
end | |
it 'swaps higher' do | |
sorted = ArrayOrdering.move_to_position(list, second, 2) | |
assert sorted.eql? [first, third, second, fourth] | |
end | |
it 'moves higher' do | |
sorted = ArrayOrdering.move_to_position(list, first, 2) | |
assert sorted.eql? [second, third, first, fourth] | |
end | |
it 'swaps with last position' do | |
sorted = ArrayOrdering.move_to_position(list, third, 3) | |
assert sorted.eql? [first, second, fourth, third] | |
end | |
it 'moves to last position' do | |
sorted = ArrayOrdering.move_to_position(list, second, 3) | |
assert sorted.eql? [first, third, fourth, second] | |
end | |
# it 'moves to last position when move index is greater then list length' do | |
# sorted = ArrayOrdering.move_to_position(list, second, 999) | |
# assert sorted.eql? [first, third, fourth, second] | |
# end | |
it 'inserts new items into the array' do | |
fifth = 5 | |
sorted = ArrayOrdering.move_to_position(list, fifth, 1) | |
assert sorted.eql? [first, fifth, second, third, fourth] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment