Skip to content
7 changes: 7 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def hello
"Hello!"
end

def greet(name="Stephen")
"Hello, #{name}!"
end
7 changes: 7 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def ftoc(f_temp)
((f_temp - 32.0) * 5.0)/9.0
end

def ctof(c_temp)
((c_temp * 9.0)/5.0)+32.0
end
38 changes: 38 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def add(a, b)
a + b
end

def subtract(a, b)
a - b
end

def sum(nums_to_sum)
total = 0
nums_to_sum.each do |num|
total += num
end

return total
end

def multiply(nums_to_multiply=[])
total = 1
nums_to_multiply.each do |num|
total *= num
end

return total
end

def power(a, b)
a**b
end

def factorial(num)
total = 1
2.upto(num) do |product|
total *= product
end

return total
end
38 changes: 29 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,41 @@

describe "#multiply" do

it "multiplies two numbers"
it "multiplies two numbers" do
expect(multiply([2,3])).to eq(6)
end

it "multiplies several numbers" do
expect(multiply([2,6,3,28])).to eq(1008)
end

it "multiplies several numbers"

end

describe "#power" do
it "raises one number to the power of another number"
it "raises one number to the power of another number" do
expect(power(5,8)).to eq(390625)
end
end

# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
it "computes the factorial of 0"
it "computes the factorial of 1"
it "computes the factorial of 2"
it "computes the factorial of 5"
it "computes the factorial of 10"
it "computes the factorial of 0" do
expect(factorial(0)).to eq(1)
end

it "computes the factorial of 1" do
expect(factorial(1)).to eq(1)
end

it "computes the factorial of 2" do
expect(factorial(2)).to eq(2)
end

it "computes the factorial of 5" do
expect(factorial(5)).to eq(120)
end

it "computes the factorial of 10" do
expect(factorial(10)).to eq(3628800)
end
end
33 changes: 33 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def echo(statement)
statement
end

def shout(statement)
statement.upcase
end

def repeat(input, n=2)
([input] * n).join(' ')
end

def start_of_word(word, num)
word.slice(0..(num - 1))
end

def first_word(input)
input.split(' ')[0]
end

def titleize(input)
captilized = []
little_words = ['and', 'the', 'over']
input.split(' ').each_with_index do |word, ind|
if ind != 0 && little_words.include?(word)
captilized.push(word)
else
captilized.push(word.capitalize)
end
end

captilized.join(' ').to_s
end
19 changes: 19 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def translate(input)
must_capitalize = true if input.capitalize == input
vowels = %w(a e i o u)
words_to_translate = input.split(' ')
translated_word = []
words_to_translate.each do |word|
if vowels.include?(word[0])
translated_word << word + 'ay'
else
ind = 0
ind += 1 until vowels.include?(word[ind]) && (word[ind-1] + word[ind] != 'qu')

translated_word << word.slice(ind...word.size) + word.slice(0...ind) + 'ay'
end
end

return translated_word.join(' ').capitalize if must_capitalize
return translated_word.join(' ')
end
5 changes: 5 additions & 0 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@

# Test-driving bonus:
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
it "must retain captilization after translation" do
s = translate("The quick brown fox")
expect(s).to eq("Ethay ickquay ownbray oxfay")
end

# * retain the punctuation from the original phrase

end
13 changes: 13 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def reverser
yield.split.map { |word| word.reverse}.join(" ")
end

def adder(num=1)
yield + num
end

def repeater(loops=1)
loops.times do
yield
end
end
10 changes: 10 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def measure(num_times = 1)
avg_time = []
num_times.times do
beginning_time = Time.now
yield
avg_time << (Time.now - beginning_time)
end

return avg_time.inject{ |total_time, bench_time| total_time + bench_time }.to_f / avg_time.size
end
6 changes: 6 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Friend
def greeting(name=nil)
return "Hello, #{name}!" if name
"Hello!"
end
end
25 changes: 25 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Book
def initialize(title = nil)
@title = title
@exclusion_words = %w(the a an and in of)
end

def title
capitalize_each(@title)
end

def title=(title)
capitalize_each(@title = title)
end

def capitalize_each(title)
capitalized_title = title.split.each_with_index.map do |v, i|
if i == 0 || !@exclusion_words.include?(v)
v.capitalize
else
v
end
end
capitalized_title.join(' ')
end
end
11 changes: 11 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Timer
attr_accessor :seconds

def initialize
self.seconds = 0
end

def time_string
Time.at(self.seconds).utc.strftime("%H:%M:%S")
end
end
63 changes: 63 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Temperature
attr_accessor :temp

def initialize(options={})
@temp = options
end

def in_fahrenheit
if temp[:f]
return temp[:f]
else
return ((temp[:c] * 9.0)/5.0)+32.0
end
end

def in_celsius
if temp[:c]
return temp[:c]
else
return ((temp[:f] - 32.0) * 5.0)/9.0
end
end

def self.from_celsius(temp)
return Temperature.new(:c => temp)
end

def self.from_fahrenheit(temp)
return Temperature.new(:f => temp)
end
end

class Celsius < Temperature
attr_accessor :temp

def initilize(temp)
@temp = temp
end

def in_celsius
return temp
end

def in_fahrenheit
return ((temp * 9.0)/5.0)+32.0
end
end

class Fahrenheit < Temperature
attr_accessor :temp

def initilize(temp)
@temp = temp
end

def in_celsius
return ((temp - 32.0) * 5.0)/9.0
end

def in_fahrenheit
return temp
end
end
38 changes: 38 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Dictionary
attr_accessor :entries

def initialize (entries = {})
@entries = entries
end

def add(words={})
if words.is_a? String
entries[words] = nil
else
words.each_pair do |word, definition|
entries[word] = definition
end
end
end

def keywords
entries.keys.sort
end

def include?(word)
entries.has_key?(word)
end

def find(word)
return entries.select{|k,v| k.start_with? word} || {}
end

def printable
printable_list = ""
entries.sort.each do |value|
printable_list << "[#{value[0]}] \"#{value[1]}\"\n"
end

printable_list.strip
end
end
Loading