Skip to content
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

Jonathan Popenuck's solution to Test First Ruby #79

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def hello
"Hello!"
end
def greet(who)
"Hello, #{who}!"
end
15 changes: 15 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def ftoc(temp)
temp = temp.to_f
temp -= 32
temp *= 5
temp /= 9
temp
end

def ctof (temp)
temp = temp.to_f
temp *= 9
temp /= 5
temp += 32
temp
end
31 changes: 31 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def add(x, y)
x + y
end

def subtract(x, y)
x - y
end

def sum(array)
if array.length == 0
0
else
array.inject{|result, n| result + n}
end
end

def multiply(array)
array.inject{|result, n| result * n}
end

def power(x, y)
x ** y
end

def factorial(number)
if number == 0
return 1
else
(1..number).inject{|result, n| result * n}
end
end
37 changes: 27 additions & 10 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,38 @@

describe "#multiply" do

it "multiplies two numbers"

it "multiplies several numbers"

it "multiplies two numbers" do
expect(multiply([3,7])).to eq(21)
end
it "multiplies several numbers" do
expect(multiply([3,7,2])).to eq(42)
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
it "returns 1 if exponent is 0" do
expect(power(1,0)).to eq(1)
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
38 changes: 38 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def echo(string)
string
end

def shout(string)
string.upcase
end

def repeat(string, n = 2)
new_string = (string + " ") * n
new_string.strip!
end

def start_of_word(string, n)
letters = string.split ''
letters[0..(n-1)].join ''
end

def first_word(string)
words = string.split ' '
words[0]
end

def titleize(string)
words = string.split ' '
little_words = ["and", "the", "over"]
if little_words.include? words[0]
words[0][0] = words[0][0].upcase
end
words.each do |word|
if little_words.include? word
next
else
word[0] = word[0].upcase
end
end
words.join ' '
end
21 changes: 21 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def translate(string)
translation = ''
vowels = ["a", "e", "i", "o", "u"]
words = string.split ' '
words.each do |word|
letters = word.split ''
until vowels.include? letters[0]
letter = letters.shift
letters.push letter
end
#if next letter is q, checks end of word for u and shifts it if necessary
if letters[0] == "u" && letters[-1] == "q"
letter = letters.shift
letters.push letter
end
letters.push "ay"
translation += (letters.join '') + ' '
end
translation.strip!
end
translate("quiet")
16 changes: 16 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def reverser
words = yield.split ' '
words.each do |word|
word.reverse!
end
words.join ' '
end

def adder(add=1)
yield + add
end

def repeater(repeat=1)
repeat.times do yield
end
end
7 changes: 7 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def measure(n=1)
start = Time.now
n.times do
yield
end
(Time.now - start) / n
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)
if name == nil
"Hello!"
else
"Hello, #{name}!"
end
end
end
Binary file added 08_book_titles/.DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Book
attr_accessor :title

def title
words = @title.split ' '
articles = ["the", "a", "an", "and", "in", "of"]
if articles.include? words[0]
words[0][0] = words[0][0].upcase
end
words.each do |word|
if articles.include? word
next
else
word[0] = word[0].upcase
end
end
words.join ' '
end
end
35 changes: 35 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Timer
attr_accessor :seconds

def initialize
@seconds = 0
end

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

def hours
hours = @seconds / 3600
end

def minutes
minutes = (@seconds - hours * 3600) / 60
end

def seconds
seconds = @seconds % 60
end

def padded(number)
number = number.to_s
digits = number.length
if digits == 0
"00"
elsif digits == 1
"0" + number
else
number
end
end
end
23 changes: 12 additions & 11 deletions 09_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@
# 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
Binary file added 10_temperature_object/.DS_Store
Binary file not shown.
42 changes: 42 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Temperature
#converts options hash into C° and F° values
def initialize(options = {})
@degreesf, @degreesc = options[:f], options[:c]

if @degreesf != nil && @degreesc == nil
@degreesc = (@degreesf - 32) * 5.0 / 9.0
elsif @degreesc != nil && @degreesf == nil
@degreesf = (@degreesc * 9.0 / 5.0) + 32
end
end
#returns converted input as F°
def in_fahrenheit
@degreesf
end
#returns converted input as C°
def in_celsius
@degreesc
end
#Factory method for inputs from C°
def self.from_celsius(temperature)
Temperature.new(:c => temperature)
end
#Factory method for inputs from F°
def self.from_fahrenheit(temperature)
Temperature.new(:f => temperature)
end
end
#Temperature subclass to convert input to C°
class Celsius < Temperature
def initialize(temperature)
@degreesc = temperature
@degreesf = (temperature * 9.0 / 5.0) + 32
end
end
#Temperature subclass to convert input to F°
class Fahrenheit < Temperature
def initialize(temperature)
@degreesf = temperature
@degreesc = (temperature - 32) * 5.0 / 9.0
end
end
51 changes: 51 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Dictionary
attr_accessor :entries

#creates empty hash for entries
def initialize
@entries = {}
end

#adds entries with key and value(if given)
def add(input)
if input.class == String
@entries[input] = nil
else
@entries.merge!(input)
end
end

#returns keys alphabetically
def keywords
@entries.keys.sort
end

#checks whether or not a given key is in @entries
def include?(word)
if @entries.keys.include?(word)
true
else
false
end
end

#finds keys and parts of keys and returns all relevant entries
def find(search)
results = {}
@entries.each do |key, value|
if key.start_with? search
results[key] = value
end
end
results
end

#sorts and prints @entries in [key] "value" format
def printable
printed = @entries.sort.map do |key,value|
"[#{key}] \"#{value}\""
end
printed.join "\n"
end

end
Loading