Last active
October 7, 2018 00:02
-
-
Save clarkngo/baa9e5446def1057266ae31171d4ad53 to your computer and use it in GitHub Desktop.
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
# other solution | |
=begin | |
def find_missing_number(sequence) | |
number_sequence = sequence.split.map(&:to_i).sort | |
number_sequence.each.with_index(1) do |actual, expected| | |
return expected unless actual == expected | |
end | |
0 | |
end | |
=end | |
# My Solution -- WORKING -- NEEDS REFACTORING | |
def find_missing_number(sequence) | |
if sequence == "" | |
return 0 | |
end | |
# remove whitespaces and convert to array | |
sequence = sequence.gsub(/\s+/, ",").split(",") | |
# string to array | |
# sequence = sequence.split(",") | |
# sort by integer value | |
sequence = sequence.sort_by(&:to_i) | |
# sort string | |
# sequence = sequence.chars.sort.join | |
puts sequence | |
if sequence[0].to_i != 1 | |
return 1 | |
elsif | |
sequence[0].to_i == 1 && sequence.length == 1 | |
return 1 | |
elsif | |
sequence.count("a-zA-Z") > 0 | |
return 1 | |
elsif | |
sequence == "" | |
return 0 | |
elsif | |
sequence[0].to_i == 2 | |
return 1 | |
else | |
for i in 1..sequence.length - 1 | |
check = sequence[i].to_i - sequence[i - 1].to_i | |
if check > 1 | |
missNum = sequence[i - 1].to_i + 1 | |
return missNum | |
end | |
end | |
if check == 1 | |
return 0 | |
end | |
end | |
end | |
find_missing_number("2 1 4 3 a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment