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
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n",
"1.7320508075688772\n",
"2.23606797749979\n",
"2.6457513110645907\n",
"3.0\n",
"3.3166247903554\n",
"3.605551275463989\n",
"3.872983346207417\n",
"4.123105625617661\n",
"4.358898943540674\n",
"4.58257569495584\n",
"4.795831523312719\n",
"5.0\n"
]
},
{
"data": {
"text/plain": [
"1..25"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1..25).step(2).each do |n|\n",
" puts n ** 0.5\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*If i starts at 1 and 2 is iteratively added to it with the step method then the integers will be odd.*"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n",
"21\n",
"23\n",
"25\n"
]
},
{
"data": {
"text/plain": [
"1..25"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1..25).step(2).each do |n|\n",
" puts n\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n",
"1.7320508075688772\n",
"2.23606797749979\n",
"2.6457513110645907\n",
"3.0\n",
"3.3166247903554\n",
"3.605551275463989\n",
"3.872983346207417\n",
"4.123105625617661\n",
"4.358898943540674\n",
"4.58257569495584\n",
"4.795831523312719\n",
"5.0\n"
]
}
],
"source": [
"i = 1\n",
"while i <= 25\n",
" puts i ** 0.5\n",
" i += 2\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]\n",
"1.7320508075688772\n",
"2.23606797749979\n",
"2.6457513110645907\n",
"3.0\n",
"3.3166247903554\n",
"3.605551275463989\n",
"3.872983346207417\n",
"4.123105625617661\n",
"4.358898943540674\n",
"4.58257569495584\n",
"4.795831523312719\n",
"5.0\n"
]
}
],
"source": [
"i = 1\n",
"odds = (1..25).step(2).to_a\n",
"puts odds\n",
"\n",
"length = odds.length\n",
"\n",
"while i < length\n",
" puts odds[i] ** 0.5\n",
" i += 1\n",
"end"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.3.0",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.3.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
(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