Skip to content

Instantly share code, notes, and snippets.

@marksim
Created July 9, 2013 15:28
Show Gist options
  • Save marksim/5958298 to your computer and use it in GitHub Desktop.
Save marksim/5958298 to your computer and use it in GitHub Desktop.
require 'rspec'
class MinNaturalNumber
def initialize(*args)
@numbers = args
end
attr_reader :numbers
def solution
array = Array.new(numbers.count)
numbers.each do |n|
array[n] = true if n < array.size
end
array.each.with_index do |present, index|
return index if !present
end
numbers.count
end
end
describe MinNaturalNumber do
it "takes a list of numbers" do
solver = MinNaturalNumber.new(3, 2, 1)
expect(solver.numbers).to eql [3, 2, 1]
end
context "can find the smallest natural (postive) number not in the set" do
it "finds 0 for [3, 2, 1]" do
solver = MinNaturalNumber.new(3, 2, 1)
expect(solver.solution).to eql 0
end
it "finds 4 for [3, 2, 0, 1]" do
solver = MinNaturalNumber.new(3, 2, 0, 1)
expect(solver.solution).to eql 4
end
it "finds 15 for [8, 23, 9, 0, 12, 11, 1, 10, 13, 7, 41, 4, 14, 21, 5, 17, 3, 19, 2, 6]" do
solver = MinNaturalNumber.new(8, 23, 9, 0, 12, 11, 1, 10, 13, 7, 41, 4, 14, 21, 5, 17, 3, 19, 2, 6)
expect(solver.solution).to eql 15
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment