This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrangeTree | |
def initialize | |
@age = 0 | |
@height = 0 | |
@orange_count = 0 | |
@alive = true | |
end | |
def height | |
if @alive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |