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
"Hello!"
end

def greet(someone)
"Hello, #{someone}!"
end
10 changes: 10 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#farenheigh to celsius
def ftoc(temperature)
celsius = (temperature.to_f - 32.0) * (5.0/9.0)
end


#celsius to fahrenheit
def ctof(temperature)
fahrenheight = (temperature.to_f * (9.0/5.0)) + 32.0
end
35 changes: 35 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# add method
def add(first, second)
first + second
end

#subtract method
def subtract(first, second)
first - second
end

#sum method
def sum(array)
sum = 0
array.each { |x| sum += x }
sum
end

#multiply method
def multiply(*numbers)
product = 1
numbers.each { |x| product *= x}
product
end

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

#factorial method
def factorial(n)
product = 1
(n).downto(1).each { |x| product *= x }
product
end
38 changes: 28 additions & 10 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,40 @@
# write tests and code for the following:

describe "#multiply" do
it "multiplies two numbers" do
expect(multiply(5,7)).to eq(35)
end

it "multiplies two numbers"

it "multiplies several numbers"

it "multiplies several numbers" do
expect(multiply(5,84,32,6)). to eq(80640)
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(4,3)). to eq(64)
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
34 changes: 34 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def translate(input)

alphabet = ("a".."z").to_a
vowels = ["a","e","i","o","u"]
consonants = alphabet - vowels
words = input.split(" ")



words.map do |word|

#word begins with vowel
if vowels.include?(word[0])
word << "ay"
#word begins with 2 consonants
elsif (consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2])) || word[1..2].include?("qu")
add = word[0..2] + "ay"
word.delete!(word[0..2])
word << add
elsif (consonants.include?(word[0]) && consonants.include?(word[1])) || word[0..1].include?("qu")
add = word[0..1] + "ay"
word.delete!(word[0..1])
word << add
#word begins with 1 consonant
elsif consonants.include?(word[0])
add = word[0] + "ay"
word.delete!(word[0])
word << add
end
end

words = words.join" ".to_s
end

10 changes: 10 additions & 0 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
expect(s).to eq("ethay ickquay ownbray oxfay")
end

it "retains capitalized words" do
s = translate("The Quick Brown Fox")
expect(s).to eq("eThay ickQuay ownBray oxFay")
end

it "retains punctuation" do
s = translate("something 'here' should still translate...!")
expect(s).to eq("omethingsay 'erehay' ouldshay illstay anslatetray...!")
end

# 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
Expand Down
13 changes: 13 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def reverser
words = yield.split" "
words.map {|word| word.reverse }.join(" ")
end

def adder(n=1)
yield(n) + 1 + (n-1)
end

def repeater(number=1)
number.times {|x| yield }
end

5 changes: 5 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def measure(n=1)
start_time = Time.now
n.times {|n| yield }
(Time.now - start_time) / n
end
11 changes: 11 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Friend

def greeting(someone=nil)
if someone == nil
"Hello!"
else
"Hello, #{someone}!"
end
end

end
18 changes: 18 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Book

attr_accessor :title

def title
@title = @title.split(" ")
articles = ["the","a","and","in","of","an"]
@title.map do |word|
word[0] = word[0].capitalize unless articles.include?(word)
end
@title[0] = @title[0].capitalize
@title.join(" ")
end


end


15 changes: 15 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Timer

attr_accessor :seconds

def seconds
0
end

def time_string
default = Time.at(0)+18000
@seconds
(default + @seconds).strftime("%H:%M:%S")
end

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

def initialize(values)
@f = values[:f]
@c = values[:c]
@c.nil? ? @c = Temperature.ftoc(@f) : @f = Temperature.ctof(@c)
end


def in_fahrenheit
@f
end

def in_celsius
@c
end

#factory methods
def self.from_celsius(values)
Temperature.new(:c => values)
end

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

#test-driving bonus:
def self.ftoc(values)
(values - 32) * (5.0/9.0)
end

def self.ctof(values)
(values * (9.0/5.0)) + 32.0
end
end


#subclasses

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

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

def initialize
@entries = {}
end

def entries
@entries
end

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

def keywords
@entries.keys.sort
end

def include?(word)
keywords.include?(word)
end

def find(word)
result = {}
@entries.each do |key, value|
if key.start_with?(word)
result[key] = value
end
end
result
end

def printable
print = @entries.sort.map do |key, value|
"[#{key}] \"#{value}\""
end
print.join"\n"
end


#class ends here
end
55 changes: 55 additions & 0 deletions 12_rpn_calculator/rpn_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class RPNCalculator

def initialize
@numbers = []
end

def push(value)
@numbers << value.to_f
end

def calc(operation)
raise "calculator is empty" if @numbers.size < 2
calculation = @numbers[-2].send(operation, @numbers[-1])
@numbers.pop(2)
@numbers << calculation
end

def plus
calc(:+)
end

def minus
calc(:-)
end

def divide
calc(:/)
end

def times
calc(:*)
end

def tokens(string)
string.split(" ").map do |item|
item =~ /[\+\-\*\/]/ ? item.to_sym : item.to_f
end
end

def evaluate(string)
tokens(string).each do |item|
item.is_a?(Float) ? @numbers<<item : calc(item)
end
value
end

def value
@numbers.last
end
end


# rewrite with everything with num1.send(operation, num2) method
# @numbers.pop(2).inject(operator)
#[1, 2, 3, 4, 5] -- [4,6].inject(:*)
Loading