Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Created February 22, 2017 22:56
Show Gist options
  • Save dmmfll/b085bb4290f51981cbbe121ee3f3e75a to your computer and use it in GitHub Desktop.
Save dmmfll/b085bb4290f51981cbbe121ee3f3e75a to your computer and use it in GitHub Desktop.
Untitled.txt
.ipynb_checkpoints/
gist_url
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def element_index(element, my_array)
i = 0
length = my_array.length
while i < length
guess = my_array[i]
# puts [guess == element, guess]
return i if guess == element
i += 1
end
-1
end
p element_index("b", ["a", "b", "c"])
# should output 1
p element_index("hello", ["a", "b", "c"])
# should output -1
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
(1..25).step(2).each do |n|
puts n ** 0.5
end
(1..25).step(2).each do |n|
puts n
end
i = 1
while i <= 25
puts i ** 0.5
i += 2
end
i = 1
odds = (1..25).step(2).to_a
puts odds
length = odds.length
while i < length
puts odds[i] ** 0.5
i += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment