Skip to content

Instantly share code, notes, and snippets.

View cnocon's full-sized avatar
:octocat:
Professionally developing

Cristin O'Connor cnocon

:octocat:
Professionally developing
View GitHub Profile
@cnocon
cnocon / die.rb
Created September 29, 2013 14:33
Creating a class of Die in ruby and allowing users to cheat or play fair :) From Chris Pine's Learn to Program book
class Die
def initialize #this will run as soon as a new instance of the Die class (a Die object) is called
puts "Do you want to cheat, or play fair? To cheat, type 'cheat' and press enter. Otherwise just press enter."
response = gets.chomp
if response == 'cheat'
cheat
else
roll
end
end
@cnocon
cnocon / orangetree.rb
Created September 29, 2013 16:27
From Chris Pine's Learn to Program: Orange tree. Make an OrangeTree class that has a height method that returns its height and a one_year_passes method that, when called, ages the tree one year. Each year the tree grows taller (however much you think an orange tree should grow in a year), and after some number of years (again, your call) the tre…
class OrangeTree
def initialize
@age = 0
@height = 0
@orange_count = 0
@alive = true
end
def height
if @alive
@cnocon
cnocon / grandfather_clock.rb
Created October 5, 2013 13:07
Grandfather clock exercise from "Learn to Program" by Chris Pine.
# Grandfather clock.
# Write a method that takes a block and calls it once for each hour that has passed today.
# That way, if I were to pass in the block:
# do
# puts 'DONG!'
# end
# it would chime (sort of) like a grandfather clock. Test your method out with a few different blocks.
# Hint: You can use Time.new.hour to get the current hour.
# However, this returns a number between 0 and 23, so you
@cnocon
cnocon / iterative.rb
Created October 26, 2013 21:41
Iterative Solution to the Problem of Determining Whether a Number is a Fibonacci Number or Not
def is_fibonacci?(input_num)
raise ArgumentError.new("Number must be greater than or equal to zero.") if input_num < 0
# we start with these variable values to generate our array of fibonacci nums
fibonacci_nums = [0,1]
i = 1
# we start with zero because this value is immediately changed in the first iteration and then incremented
# it only has to be less than the input_num so the loop runs at least once
@cnocon
cnocon / gist:7174944
Created October 26, 2013 21:46
Non-Iterative, Non-Recursive Solution for Identifying Whether a Number is a Fibonacci Number
def is_fibonacci?(i)
n1 = (5*i**2 + 4)**0.5
n2 = (5*i**2 - 4)**0.5
n1 % 1 == 0 || n2 % 1 == 0 ? true : false
end
@cnocon
cnocon / recursive_memoization_fibonacci.rb
Created October 26, 2013 21:56
Recursive Solution to Fibonacci Problem (Using Memoization)
def is_fibonacci?(i)
n = 1
fib = 1
while fib <= i
fib = recursive_nth_fibonacci(n)
return true if fib == i
n += 1
end
false
end
@cnocon
cnocon / memoization.rb
Last active December 26, 2015 16:48
For blog post on memoization and recursion
# When this method is called on any class instance,
# the method will reconstruct the string every time.
def user_full_name
"#{first_name} #{last_name}"
end
# By memoizing the call, we store the string with the
# object that is the class instance. Below is a classic
# example of memoization in code:
@cnocon
cnocon / 0.2.1-boggle_class_from_methods.rb
Last active December 27, 2015 17:29 — forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize (board)
@board=board
end
def create_word(*coords)
puts coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
@cnocon
cnocon / gist:7705368
Created November 29, 2013 12:58
Another look at why it's important to understand what the method you are using is returning and how it works. There are some significant advantages and disadvantages to methods that might generate the same, correct, output for you.
require 'benchmark'
array = (1..900_000).to_a
Benchmark.bmbm do |x|
#reject returns the array without the rejected number, so it has to re-index it without the rejected number
x.report("reject 890,000") { array.reject{|num| num == 890_000}}
x.report("reject 2") { array.dup.reject{|num| num == 2}}
#select goes through whole array no matter what bc it returns all matched elements
x.report("select 890,000") { array.select{|num| num == 890_000}}
x.report("select 2") { array.select{|num| num == 2}}
#detect returns the first match for what's in the block, so the later the match, the longer it will take
@cnocon
cnocon / blogpost.html
Last active December 31, 2015 01:09
Blog post on javascript iteration
<!DOCTYPE html>
<html>
<head>
<title>Looping in Javascript</title>
<script async src="blogpost.js"></script>
</head>
<body>
<section>
<h2>Here is our 1st section element.</h2>
</section>