Skip to content

test-first ruby #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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
"Hello!"
end

def greet(name)
"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(temperature)
(temperature - 32) * (5.0 / 9.0)
end

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

def subtract(a, b)
a - b
end

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

def multiply(*numbers)
numbers.reduce(1, :*)
end

def power(base, exponent)
if exponent == 0
return 1
elsif exponent % 2 == 0
return power(base * base, exponent / 2)
else
return base * power(base * base, (exponent - 1) / 2)
end
end

def factorial(n)
(2..n).to_a.reduce(1, :*)
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(5, 2)).to eq(10)
end

it "multiplies several numbers"
it "multiplies several numbers" do
expect(multiply(5, 2, 3)).to eq(30)
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(5, 15)).to eq(30517578125)
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
31 changes: 31 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def echo(message)
message
end

def shout(message)
message.upcase
end

def repeat(message, times = 2)
("#{message} " * times).strip
end

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

def first_word(sentence)
sentence.split.first
end

def titleize(sentence)
little_words = ["and", "over", "the"]
title_case_array = sentence.split.map.with_index do |word, i|
if (i != 0) and little_words.include?(word)
word
else
word.capitalize
end
end
title_case_array.join(" ")
end
28 changes: 28 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def translate(sentence)
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = ('a'..'z').to_a.select {|letter| not vowels.include?(letter)}
consonants.push("qu")

pig_latin_array = sentence.split.map do |word|
result = word
consonant_cluster = ""

while not vowels.include?(result[0]) and result.length > 0 do
consonant_cluster += result[0]
result = result[1..-1]
end

if result.length > 0
overlap = "#{consonant_cluster[-1]}#{result[0]}"

if consonants.include?(overlap)
consonant_cluster += result[0]
result = result[1..-1]
end
end

"#{result}#{consonant_cluster}ay"
end

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

def adder(number = 1)
yield + number
end

def repeater(repetitions = 1)
repetitions.times { yield }
end
14 changes: 14 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def measure(repetitions = 1)
measurements = []
repetitions.times do
elapsed_time = run_and_time_block { yield }
measurements.push(elapsed_time)
end
measurements.reduce(:+) / measurements.length
end

def run_and_time_block
start = Time.now
yield
Time.now - start
end
9 changes: 9 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Friend
def greeting(name = nil)
message = "Hello!"
if name
message = "Hello, #{name}!"
end
message
end
end
14 changes: 14 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Book
attr_reader :title

def title=(new_title)
@title = new_title.split.map.with_index do |word, index|
(index == 0 or capitalize?(word)) ? word.capitalize : word
end.join(" ")
end

private def capitalize?(word)
never = ["a", "an", "and", "in", "of", "the"]
not never.include?(word)
end
end
19 changes: 19 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Timer
attr_accessor :seconds

def initialize
@seconds = 0
end

def time_string
hours = @seconds / (60 * 60)
minutes = (@seconds - (hours * 60 * 60)) / 60
seconds = @seconds - ((hours * 60 * 60) + (minutes * 60))

"#{padded(hours)}:#{padded(minutes)}:#{padded(seconds)}"
end

def padded(value)
"%02d" % value
end
end
24 changes: 12 additions & 12 deletions 09_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it "should display 0 seconds as 00:00:00" do
@timer.seconds = 0
expect(@timer.time_string).to eq("00:00:00")
end
end

it "should display 12 seconds as 00:00:12" do
@timer.seconds = 12
Expand All @@ -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
46 changes: 46 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Temperature
def initialize(options = {})
@fahrenheit = nil
if options.key?(:f)
@fahrenheit = options[:f]
elsif options.key?(:c)
@fahrenheit = self.class.ctof(options[:c])
end
end

def self.from_fahrenheit(temperature)
new(:f => temperature)
end

def self.from_celsius(temperature)
new(:c => temperature)
end

def self.ftoc(temperature)
(temperature - 32) * (5.0 / 9.0)
end

def self.ctof(temperature)
(temperature * (9.0 / 5.0)) + 32
end

def in_fahrenheit
@fahrenheit
end

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

class Celsius < Temperature
def initialize(celsius)
super(:c => celsius)
end
end

class Fahrenheit < Temperature
def initialize(fahrenheit)
super(:f => fahrenheit)
end
end
44 changes: 44 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Dictionary
attr_reader :entries

def initialize
@entries = {}
end

def add(value)
if value.is_a?(Hash)
value.each do |word, definition|
@entries[word] = definition
end
elsif value.is_a?(String)
@entries[value] = nil
end
end

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

def find(prefix)
result = {}
@entries.each do |word, definition|
if word =~ /^#{Regexp.quote(prefix)}/
result[word] = definition
end
end
result
end

def keywords
@entries.keys.sort
end

def printable
output = ""
@entries.keys.sort.each_with_index do |word, index|
output += %Q{[#{word}] "#{@entries[word]}"}
output += "\n" if index < @entries.length - 1
end
output
end
end
Loading