Last active
April 25, 2019 10:17
-
-
Save KevinSia/e8bf32b0da2791f03bd03c5c704a9814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def linear_search(target, arr) | |
for i in 0...arr.length | |
return i if target == arr[i] | |
end | |
return nil | |
end | |
def global_linear_search(target, arr) | |
indices = [] | |
for i in 0...arr.length | |
indices << i if target == arr[i] | |
end | |
indices | |
end | |
# Implement the #linear_search method | |
random_numbers = [6, 29, 18, 2, 72, 19, 18, 10, 37] | |
p linear_search(37, random_numbers) | |
# Implement the #global_linear_search method | |
bananas_arr = "bananas".split(//) | |
p global_linear_search('n', bananas_arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment