Skip to content

Instantly share code, notes, and snippets.

@KevinSia
Last active April 25, 2019 10:17
Show Gist options
  • Save KevinSia/e8bf32b0da2791f03bd03c5c704a9814 to your computer and use it in GitHub Desktop.
Save KevinSia/e8bf32b0da2791f03bd03c5c704a9814 to your computer and use it in GitHub Desktop.
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