Skip to content

Completed exercises (except for #13 and #15) #98

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 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5e3a50e
finished 00_hello
Jan 22, 2017
9b308c9
completed 01_temperature
Jan 22, 2017
d333f66
completed minimum requirements for 02_calculator
Jan 23, 2017
7199b20
completed tests and code for #multiply
Jan 23, 2017
b7b3249
completed tests and code for #power
Jan 23, 2017
49f5cc6
completed tests and code for #factorial
Jan 23, 2017
3eff41c
completed echo method
Jan 23, 2017
39e5334
completed shout method
Jan 23, 2017
bdcbc2f
completed repeat method
Jan 23, 2017
5d56219
completed start_of_word method
Jan 23, 2017
fff00c4
completed first_word method
Jan 23, 2017
ee04229
completed titleize method
Jan 23, 2017
397b975
completed code for all tests in pig_latin_spec.rb
Jan 23, 2017
364350e
completed extra credit problem to capitalize translated words
Jan 23, 2017
10b80cc
completed reverser method
Jan 23, 2017
c5d5e3e
modified reverser method to only run if block is provided to it
Jan 23, 2017
62764b0
completed adder method
Jan 23, 2017
3dd15dc
completed repeater method
Jan 23, 2017
fecdce8
completed measure method
Jan 23, 2017
cd445a6
refactored measure method
Jan 23, 2017
733048b
completed Friend class and greeting method
Jan 23, 2017
977a462
completed Book class and title attr_accessor
Jan 23, 2017
b7ff10a
initial class and method definitions
Jan 23, 2017
1ddaf83
uncommenting specs for helper method
Jan 23, 2017
5bc9124
completed time_string and padded methods
Jan 23, 2017
aad72b0
completed Temperature class
Jan 23, 2017
54d8830
completed Celsius and Fahrenheit subclasses
Jan 23, 2017
d01b00e
completed Dictionary class and required methods
Jan 24, 2017
0b6ccab
refactored find and printable methods
Jan 24, 2017
4a133f6
first working implementation of value method
Jan 25, 2017
43ac811
completed tokens method
Jan 25, 2017
ddbe425
completed evaluate method
Jan 25, 2017
d0477d4
completed hello method
Jan 25, 2017
98ee8f6
finished send method
Jan 25, 2017
c519d98
modified hello method
Jan 25, 2017
5cecec8
added goodbye method
Jan 25, 2017
0e145a6
replaced goodbye method with method_missing method
Jan 25, 2017
9916d64
rewrote send method, removed hello method since it's no longer required
Jan 25, 2017
aebcd8a
first attempt at methods to indent output
Jan 26, 2017
6debb8e
completed exercise
Jan 26, 2017
976edc1
removed incorrect attempt at indenting output
Jan 26, 2017
d16f514
first attempt at solution with naive algorithm
Jan 26, 2017
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(person)
"Hello, #{person}!"
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
end

def ctof(c)
(c * 9.0 / 5) + 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.inject(0) { |sum, number| sum + number }
end

def multiply(numbers)
numbers.inject(1) { |product, number| product * number }
end

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

def factorial(number)
raise "Invalid input" if number < 0

1 if number == 0 || number == 1

result = 1
number.downto(1) { |n| result *= n }
result
end
42 changes: 32 additions & 10 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,44 @@
# write tests and code for the following:

describe "#multiply" do
it "multiplies two numbers" do
expect(multiply([2, 5])).to eq(10)
expect(multiply([5, 10])).to eq(50)
end

it "multiplies two numbers"

it "multiplies several numbers"

it "multiplies several numbers" do
expect(multiply([1, 2, 3])).to eq(6)
expect(multiply([0, 5, 10])).to eq(0)
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, 2)).to eq(4)
expect(power(5, 2)).to eq(25)
expect(power(10, 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
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(phrase)
phrase
end

def shout(phrase)
phrase.upcase
end

def repeat(phrase, num_times=2)
output = (" " + phrase) * num_times
output.strip
end

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

def first_word(sentence)
sentence.split[0]
end

def titleize(sentence)
excluded_words = %w(a an the for and nor but or yet so
at around by after along for from of
on to with without over)

words = sentence.split(" ")
words.map { |word| word.capitalize! unless excluded_words.include?(word) }
words[0].capitalize!
words.join(" ")
end
66 changes: 66 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
def translate(sentence)
words = sentence.split
words = words.map { |word| translate_word(word) }
words = words.map { |word| correct_capitalization(word) }
words.join(" ")
end

def translate_word(word)
vowels = %w(a e i o u)
ay_sound = "ay"

# word starts with a vowel
if vowels.include?(word[0])
return word + ay_sound
end

# word contains "qu" phoneme
if has_qu_phoneme?(word)
qu_index = word.index(/[Qq]u/)

# word starts with "qu"
if qu_index == 0
return word[2..-1] + word[qu_index..qu_index + 1] + ay_sound
else
# "qu" is preceded by another character, check if it's a consonant
if !vowels.include?(word[qu_index - 1])
return word[qu_index + 2..-1] + word[qu_index - 1..qu_index + 1] + ay_sound
end
end
end

# word starts with a variable number of consonants (up to 3)
case num_starting_consonants(word)
when 1
return word[1..-1] + word[0] + ay_sound
when 2
return word[2..-1] + word[0..1] + ay_sound
when 3
return word[3..-1] + word[0..2] + ay_sound
end
end

# check how many consonants the word starts with
def num_starting_consonants(word)
vowels = %w(a e i o u)
count = 0

word.downcase.chars.each do |char|
vowels.include?(char) ? break : count += 1
end

count
end

# check if word has the "qu" phoneme
def has_qu_phoneme?(word)
word.downcase[0..2].include?("qu")
end

def correct_capitalization(word)
if word.downcase != word
return word.downcase.capitalize
else
return word
end
end
8 changes: 8 additions & 0 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,13 @@
# 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 "ensures that words that were originally capitalized are still capitalized (with a different initial capital letter, of course)" do
s = translate("The Quick Brown Fox")
expect(s).to eq("Ethay Ickquay Ownbray Oxfay")
end

xit "retains the punctuation from the original phrase" do
s = translate("The Good, The Bad, and The Ugly")
expect(s).to eq("Ethay Oodgay, Ethay Adbay, anday Ethay Uglyay")
end
end
34 changes: 34 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def reverser
if block_given?
block = yield
if block.split(" ").length > 1
strings = block.split(" ")
strings.map { |string| string.reverse! }
return strings.join(" ")
else
return block.reverse
end
end
end

def adder(input=0)
if block_given?
block = yield

if input == 0
return block + 1
else
return input + 5
end
end
end

def repeater(num_times=1)
if block_given?
if num_times == 1
yield
else
num_times.times { yield }
end
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(passes = 1)
if block_given?
start_time = Time.now
passes.times { |pass| yield }
return (Time.now - start_time) / passes.to_f
end

start_time = Time.now
Time.now - start_time
end
5 changes: 5 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Friend
def greeting(person = nil)
person ? "Hello, #{person}!" : "Hello!"
end
end
20 changes: 20 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Book
attr_accessor :title

def title
words_always_in_lowercase = %w(the a an and in the of)

words_in_title = @title.split
words_in_title.each do |word|
if words_always_in_lowercase.include?(word)
next
else
word.capitalize!
end
end

words_in_title[0].capitalize!

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

def initialize
@seconds = 0
end

def time_string
hours = @seconds / 60 / 60
minutes = @seconds / 60 % 60
seconds = @seconds % 60

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

def padded(number)
if number.to_s.length == 2
number.to_s
else
"0#{number}"
end
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
Loading