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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / birthday_helper.rb
Last active March 8, 2022 00:47
Birthday helper exercise from Chris Pine's book, Learn to Program
# Write a program to read in names and birth dates from a text file.
# It should then ask you for a name. You type one in, and it tells you
# when that person’s next birthday will be (and, for the truly adventur-
# ous, how old they will be). The input file should look something like this:
# Christopher Alexander, Oct 4, 1936
# Christopher Lambert, Mar 29, 1957
# Christopher Lee, May 27, 1922
# Christopher Lloyd, Oct 22, 1938
# Christopher Pine, Aug 3, 1976
# Christopher Plummer, Dec 13, 1927
@cnocon
cnocon / englishnum.rb
Last active December 23, 2015 14:59
The english number problem from Learn to Program by Chris Pine.
# This program should take a number, like 22, and return the English version of it
#(in this case, the string 'twenty-two').
#For now, let’s have it work only on integers from 0 to 99999:
puts "Provide a number greater than or equal to 0 but less than 100,000"
num = gets.chomp.to_i
def englishnum number
if number < 0 || number > 99999
puts "Please enter a number greater than or equal to 0 but less than 100,000"
end
@cnocon
cnocon / shuffle.rb
Created September 12, 2013 13:33
My shuffle exercise for Ch. 10 in "Learning to Program" by Chris Pine
start_array = []
puts "Enter as many words as you like, pressing Enter after each word has been input. type 'stop' to finish entering words."
while true
x = gets.chomp.downcase #had to put this within the while loop for code to work, not outside while loop
if x != "stop"
start_array.push x
else
def do_shuffle start_array
shuffle start_array, []
end