-
-
Save emberlzhang/3862467 to your computer and use it in GitHub Desktop.
Solution for Linear Search with Scott
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
#linear search | |
def linear_search(object, array) | |
index = 0 | |
mismatch = 0 | |
while index < array.length | |
if object != array[index] | |
mismatch += 1 | |
end | |
if mismatch == array.length | |
return nil | |
end | |
if object == array[index] | |
index | |
break | |
end | |
index += 1 | |
end | |
index | |
end | |
#global search | |
def linear_search(object, array) | |
index = 0 | |
mismatch = 0 | |
matched_elements = [] | |
while index < array.length | |
if object != array[index] | |
mismatch += 1 | |
end | |
if mismatch == array.length | |
return nil | |
end | |
if object == array[index] | |
matched_elements << index | |
end | |
index += 1 | |
end | |
matched_elements | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment