Skip to content

Instantly share code, notes, and snippets.

@troyleach
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyleach/41c86113e2816994bd2b to your computer and use it in GitHub Desktop.
Save troyleach/41c86113e2816994bd2b to your computer and use it in GitHub Desktop.
find the smallest number and MOVE it to the front of the array
# given an array of numbers find the smallest number.
# return the same array with the lowest number at the front
#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)
copyArray = array.sort
lowest_number = copyArray[0]
array.each_with_index do |value, index|
if value == lowest_number
array.delete_at(index)
end
end
return array.unshift(lowest_number)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment