Created
June 3, 2015 13:57
-
-
Save troyleach/ac34af3a8fa780e35da7 to your computer and use it in GitHub Desktop.
Find the smallest number and MOVE it to the front - 2
This file contains 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
# find the smalledst number in a given array of numbers | |
# then MOVE that number to the front of the array | |
# return the that same array | |
#RSpec | |
describe 'smallest_number_move_to_the_front' do | |
let(:numbers) { [24,6,8,3,234,6,7,65] } | |
let(:numbers2) { [24,6,8,234,6,7,65,734,-1,322,324,43213] } | |
it "method is defined" do | |
expect(defined? smallest_number_move_to_the_front).to eq 'method' | |
end | |
it "method takes a single argument" do | |
expect(method(:smallest_number_move_to_the_front).arity).to eq 1 | |
end | |
it 'returns a new array with the lowest number first' do | |
expect(smallest_number_move_to_the_front(numbers)).to eq [3,24,6,8,234,6,7,65,] | |
end | |
it 'returns a new array with the lowest number first' do | |
expect(smallest_number_move_to_the_front(numbers2)).to eq [-1,24,6,8,234,6,7,65,734,322,324,43213] | |
end | |
end | |
# CODE | |
def smallest_number_move_to_the_front(array) | |
lowest_number = array.select.min | |
array.delete(lowest_number) | |
array.unshift(lowest_number) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment