Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
return "Hello!"
end

def greet(name)
return "Hello, #{name}!"
end
8 changes: 8 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#write your code here
def ftoc(f)
((f-32)/1.8).round(2)
end

def ctof(c)
(c*1.8 + 32).round(2)
end
26 changes: 26 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def add(num1,num2)
num1 + num2
end

def subtract(num1, num2)
num1 - num2
end

def sum(array)
array.reduce(0, :+)
end

def multiply(*nums)
nums.reduce(:*)
end

def power(base, exp)
base**exp
end

def factorial(num)
return 1 if num == 0
result = 1
num.downto(1) { |n| result *= n }
result
end
36 changes: 28 additions & 8 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(1,3)).to eq(3)
end

it "multiplies several numbers"
it "multiplies several numbers" do
expect(multiply(1,2,3)).to eq(6)
end

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(2,3)).to eq(8)
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
32 changes: 32 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def echo(what)
what
end

def shout(what)
what.upcase
end

def repeat(what, num = 2) # equal sign makes it an optional param with a default value
((what + " ")*num).strip
end

def start_of_word(word, numChars = 1)
word[0..numChars-1]
end

def first_word(someString)
someString.split.first
end

def titleize(someString)
array = someString.capitalize.split

array.map! { |e|
if e == "and" || e == "or" || e == "to" || e == "the"
e #do nothing
else
e.capitalize
end
}
array.join(" ")
end
2 changes: 1 addition & 1 deletion 03_simon_says/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
end

it "does capitalize 'little words' at the start of a title" do
expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai")
expect(titleize("the bridge over the river kwai")).to eq("The Bridge Over the River Kwai")
end
end

Expand Down
32 changes: 32 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def translate(someString)
words = someString.scan(/[a-zA-Z]+/)
pig_latin = someString.dup

words.each do |word|
capitalized = false

case word[0].downcase
when 'a','e','i','o','u'
pig_latin.gsub!(word, word + "ay")
else
capitalized = true if /[[:upper:]]/.match(word[0])

pig_latin_word = word.dup

pig_latin_word.gsub!(/qu|Qu/, '*')

index = pig_latin_word.downcase.index(/[aeiou]/)
firstPart = pig_latin_word[0..index-1]

pig_latin_word = ( pig_latin_word[index..-1]+firstPart+"ay" )

pig_latin_word.gsub!('*', "qu")

pig_latin_word.capitalize! if capitalized

pig_latin.gsub!(word, pig_latin_word)
end
end

pig_latin
end
10 changes: 9 additions & 1 deletion 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@

# Test-driving bonus:
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
# * retain the punctuation from the original phrase
it "keeps capitalized words capitalized" do
s = translate("the Quick Brown fox")
expect(s).to eq("ethay Ickquay Ownbray oxfay")
end

# * retain the punctuation from the original phrase
it "retains the original punctuation" do
s = translate("the quick, brown, fox!")
expect(s).to eq("ethay ickquay, ownbray, oxfay!")
end
end
11 changes: 11 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def reverser
yield.split.map { |word| word.reverse }.join(" ")
end

def adder(n = 1)
yield + n
end

def repeater(n = 1)
n.times { yield }
end
6 changes: 6 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def measure(n = 1)
t = Time.now
n.times { yield }

(Time.now - t)/n
end
7 changes: 7 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Friend

def greeting(name = nil)
return "Hello!" if name.nil?
"Hello, #{name}!"
end
end
24 changes: 24 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Book
attr_reader :title

def title=(str)
@title = titleize(str)
end

def titleize(someString)
words = someString.split

words[0].capitalize!

words.map! { |e|
case
when e == "i" then e.capitalize
when e == "and" || e == "the" then e #do nothing
when e.length <= 2 then e #do nothing
else e.capitalize
end
}

words.join(" ")
end
end
18 changes: 18 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Timer
attr_accessor :seconds

def initialize
@seconds = 0
end

def time_string
h = (seconds/3600).to_s.rjust(2, '0')
m = ( (seconds%3600)/60 ).to_s.rjust(2, '0')
s = ( (seconds%3600)%60 ).to_s.rjust(2, '0')
h + ":" + m + ":" + s
end

def padded(n)
"%02d" % n
end
end
22 changes: 11 additions & 11 deletions 09_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
# describe 'padded' do
# it 'pads zero' do
# expect(@timer.padded(0)).to eq('00')
# end
# it 'pads one' do
# expect(@timer.padded(1)).to eq('01')
# end
# it "doesn't pad a two-digit number" do
# expect(@timer.padded(12)).to eq('12')
# end
# end
describe 'padded' do
it 'pads zero' do
expect(@timer.padded(0)).to eq('00')
end
it 'pads one' do
expect(@timer.padded(1)).to eq('01')
end
it "doesn't pad a two-digit number" do
expect(@timer.padded(12)).to eq('12')
end
end

end
45 changes: 45 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Temperature

def initialize(options)
@temp = options.fetch(:f) if options.has_key?(:f)
@temp ||= self.class.ctof(options.fetch(:c,0))
end

def in_fahrenheit
@temp
end

def in_celsius
self.class.ftoc(@temp)
end

def self.from_celsius(c)
self.new({ c: c })
end

def self.from_fahrenheit(f)
self.new({ f: f })
end

def self.ftoc(f)
((f-32)/1.8).round(2)
end

def self.ctof(c)
(c*1.8 + 32).round(2)
end

end

class Celsius < Temperature
def initialize(c)
super({ c: c })
end
end

class Fahrenheit < Temperature
def initialize(f)
super({ f: f })
end
end

8 changes: 8 additions & 0 deletions 10_temperature_object/temperature_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
#
describe "utility class methods" do

it "converts fahrenheit to celsius" do
expect(Temperature.ftoc(32)).to eq(0)
end

it "converts celsius to faharenheit" do
expect(Temperature.ctof(0)).to eq(32)
end

end

# Here's another way to solve the problem!
Expand Down
30 changes: 30 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Dictionary
attr_accessor :entries

def initialize
@entries = Hash.new(nil)
end

def add(entry)
@entries.merge!(entry) if entry.is_a? Hash
@entries[entry] = nil if entry.is_a? String
end

def keywords
@entries.keys.sort
end

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

def find(key)
@entries.select { |word, definition| word.match(key) }
end

def printable
keywords.map do |keyword|
"[#{keyword}] \"#{@entries[keyword]}\"\n"
end.join.chomp
end
end
Loading