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

John Sawyer's Learn Ruby Assignment #78

Open
wants to merge 11 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(who)
"Hello, #{who}!"
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)
(f - 32) * 5.0 / 9.0
end

def ctof(c)
(c * 9.0 / 5.0) +32
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(a,b)
a+b
end

def subtract(a,b)
a-b
end

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

def multiply(*args)
total = 1
args.each do |num|
total *= num
end
total
end

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

def factorial(num)
(1..num).inject(:*) || 1
end
39 changes: 27 additions & 12 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,38 @@
# once the above tests pass,
# write tests and code for the following:

describe "#multiply" do
describe "multiply" do

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

it "multiplies several numbers"

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

describe "#power" do
it "raises one number to the power of another number"
describe "power" do
it "raises one number to the power of another number" do
expect(power(3,4)).to eq(81)
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"
describe "factorial" do
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
40 changes: 40 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def echo(word)
word
end

def shout(word)
word.upcase
end

def repeat(word, cycles=2)
total = word
(cycles - 1).times do
total += " " + word
end
total
end

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

def first_word(phrase)
arr = phrase.split(" ")
arr[0]
end

def titleize(phrase)
little_words = ["and", "over", "the"]
arr = phrase.split(" ")
return_string = arr.shift.capitalize
arr.each do |word|
if little_words.include?(word)
add_word = word
else
add_word = word.capitalize
end
# add_word = word.capitalize unless little_words.include?(word)
return_string += " " + add_word
end
return_string
end
27 changes: 27 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def translate(words)
def translate_word(word)
def move_first_to_last(starts_w_cons)
move_front = starts_w_cons[0]
starts_w_cons[0] = ""
if move_front == "q"
move_front += "u"
starts_w_cons[0] = ""
end
starts_w_cons + move_front
end
vowels = ["a", "e", "i", "o", "u", "y"]

until vowels.include?(word[0])
word = move_first_to_last(word)
end

word + "ay"
end

words_array = words.split(" ")
return_string = ""
words_array.each do |w|
return_string += translate_word(w) + " "
end
return_string.strip
end
33 changes: 33 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# def reverser(&block)
# block.each do |b|
# call.reverse
# end
# end

# def reverser
# yield.reverse
# end

# def reverser(&block)
# block.call.reverse
# end


def reverser
arr = yield.split(" ")
return_string = ""
arr.each do |word|
return_string += word.reverse + " "
end
return_string.strip
end

def adder(increase=1)
yield + increase
end

def repeater(multiplier = 1)
multiplier.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(cycles = 1)
counts = 0
cycles.times do
start_time = Time.now
yield
end_time = Time.now
counts += (elapsed_time = end_time - start_time)
end
counts.to_f/cycles.to_f
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(who = nil)
if who.nil?
"Hello!"
else
"Hello, #{who}!"
end
end
end
27 changes: 27 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Book
def title
@title
end

def title=(title)
@title = titlieze(title)
end

private
def titlieze(title)
little_words = ["the", "a", "in", "of", "and", "an"]
arr = title.split(" ")
return_string = arr.shift.capitalize

arr.each do |word|
if little_words.include?(word)
add_word = word
else
add_word = word.capitalize
end
return_string += " " + add_word
end
return_string
end

end
26 changes: 26 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Timer
attr_accessor :seconds
def initialize
@seconds = 0
end

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

def hours
seconds / 3600
end

def minutes
(seconds % 3600)/60
end

def remainder_seconds
(seconds % 60)
end

def padded(number)
"#{number}".rjust(2, '0')
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 @@ -44,17 +44,17 @@
# One way to implement the Timer is with a helper method.
# 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
40 changes: 40 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Temperature
def initialize(temp_hash)
@f = temp_hash[:f]
@c = temp_hash[:c]
end

def in_fahrenheit
@f ||
(@c * 9.0 / 5.0) +32
end

def in_celsius
@c ||
(@f - 32) * 5.0 / 9.0
end


class << self
def from_fahrenheit temp
Temperature.new({f: temp})
end

def from_celsius temp
Temperature.new({c: temp})
end
end
end


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

class Fahrenheit < Temperature
def initialize(temp)
super(f: temp)
end
end
34 changes: 34 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Dictionary
def initialize(defs = {})
@defs = defs
end

def entries
@defs
end

def add(defs)
if defs.is_a?(String)
@defs.merge!(defs => nil)
else
@defs.merge!(defs)
end
end

def keywords
@defs.keys.sort
end

def include?(key)
@defs.has_key?(key)
end

def find(key)
@defs.select { |word,defin| word.scan(key).join == key }
end

def printable
defs_sorted = @defs.sort_by { |word, defin| word}
defs_sorted.map{ |word, defin| "[#{word}] \"#{defin}\"\n" }.join.chomp
end
end
Loading