Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created June 8, 2017 23:52
Show Gist options
  • Save kenmazaika/e0f9ea68ddbbb3ebce638487b06c04d4 to your computer and use it in GitHub Desktop.
Save kenmazaika/e0f9ea68ddbbb3ebce638487b06c04d4 to your computer and use it in GitHub Desktop.
def find_missing_number(sequence)
# sort the numbers in ascending numerical order
# non-string values will be at the beginning
# will be the value 0. This doesn't modify
# the actual array (convert it to an integer)
# it just uses this to map to the array.
numbers = sequence.split.sort do |x, y|
x.to_i <=> y.to_i
end
prev = 0
numbers.each do |number|
puts number.inspect
if number.to_i.to_s != number
return 1
end
if number.to_i != prev + 1
return prev + 1
end
prev = prev + 1
end
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment