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
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
# 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
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
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?(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
# 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
class BoggleBoard | |
def initialize (board) | |
@board=board | |
end | |
def create_word(*coords) | |
puts coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
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
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 |
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
<!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> |