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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
BUNDLE_PATH: "vendor/bundle"
BUNDLE_DISABLE_SHARED_GEMS: "true"
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
6 changes: 6 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def ftoc farenheit
(5/9.0)*(farenheit-32)
end
def ctof celcius
(9/5.0)*celcius+32
end
26 changes: 26 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def add num,mod
num + mod
end
def subtract num, mod
num - mod
end
def sum numbers
total = 0
numbers.each{|num| total += num}
total
end
def multiply num, factor, *multiple
if multiple
multiple.each {|additional_factor| factor *= additional_factor}
end
num * factor
end
def power num, factor
num**factor
end
def factorial num
if num < 2 then return 1 end
total = 1
num.downto(1) {|factorial| total *= factorial}
total
end
33 changes: 25 additions & 8 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 two numbers" do
expect(multiply(4,9)).to eq(36)
end

it "multiplies several numbers"
it "multiplies several numbers" do
expect(multiply(4,9,0)).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,4)).to eq(16)
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(5*4*3*2*1)
end
it "computes the factorial of 10" do
expect(factorial(10)).to eq(10*9*8*7*6*5*4*3*2*1)
end
end
30 changes: 30 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def echo words
words
end
def shout words
echo words.upcase
end
def repeat words, times=2
echo words+(" "+words)*(times-1)
end
def start_of_word word, num
letters = word.split("")
start = letters[0...num]
start.join
end
def first_word sentence
words = sentence.split(" ")
words[0]
end
def titleize sentence
small = ["and", "or", "the", "over", "to", "the", "a", "but"]
i = 0
words= sentence.split(" ").map! do |word|
i += 1
if (small.include?(word) && i != 1)
word
else
word.capitalize
end
end.join(" ")
end
19 changes: 19 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def vowel? letter
["a", "e", "i", "o", "u"].include? letter.downcase
end
def qu? word, letter
(word.chars[letter].downcase == "q" && word.chars[letter+1].downcase == "u")
end
def translate sentence
words = sentence.split(" ").map do |word|
if vowel? word.chars.first then word+"ay"
else
letter = 0
until vowel? word.chars[letter+1] do
letter += 1
end
if qu? word, letter then letter += 1 end
word.chars[letter+1..-1].join+word.chars[0..letter].join+"ay"
end
end.join(" ")
end
9 changes: 9 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def reverser
yield.split(" ").map {|word| word.reverse}.join(" ")
end
def adder num=1
yield+num
end
def repeater num=1
num.times {yield}
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 num=1
nt = Time.now
num.times do
yield
end
total = (Time.now - nt)/num
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 who=nil
@greeting = "Hello #{(who ? ', ' + who : nil)}!"
end
end
29 changes: 29 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Book
def conjunction? word
["and", "but", "for", "nor", "or", "so", "yet"].include? word
end
def preposition? word
["a", "an", "in", "of", "the"].include? word


end
def title=(title)
i=0
@title = title.split(" ").map do |word|
if i == 0
i = 1
word.capitalize
elsif conjunction? word
word
elsif preposition? word
word
else
word.capitalize
end

end.join(" ")
end
def title
@title
end
end
22 changes: 22 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Timer
def initialize
self.seconds=(0)
end
def seconds=(seconds)
@seconds = seconds
@time_string = Time.at(seconds).utc.strftime("%H:%M:%S")

#Manual Method

#hours = seconds/3600
#minutes = seconds % 3600 / 60
#seconds = seconds % 3600 % 60
#@time_string = "#{(hours<10 ? '0'+hours.to_s : hours)}:#{(minutes<10 ? '0'+minutes.to_s : minutes)}:#{(seconds<10 ? '0'+seconds.to_s : seconds)}"
end
def seconds
@seconds
end
def time_string
@time_string
end
end
49 changes: 49 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Temperature
attr_accessor :f, :c

def Temperature.from_celsius temp
Temperature.new(:c => temp)
end

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

def initialize args
@f = args[:f]
@c = args[:c]
if @f
@c = ftoc
else
@f = ctof
end
end

def ftoc
(@f-32)*5/9.0
end

def ctof
(@c*9/5.0)+32
end

def in_fahrenheit
@f
end

def in_celsius
@c
end
end

class Celsius < Temperature
def Celsius.new args
Temperature.new(:c => 50)
end
end

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

attr_reader :entries

def initialize
@entries = {}
end

def add items
if items.is_a?(Hash)
items.each {|key, definition| @entries[key] = definition}
else
@entries[items] = nil
end
@entries = Hash[@entries.sort_by {|key, definition| key}]
end

def find partial
@entries.select {|key, definition| key.include? partial}
end

def include? key
@entries.has_key? key
end

def keywords
@entries.keys
end

def printable
output = ""
@entries.each do |key, definition|
output += "[#{key}] \"#{definition}\"\n"
end
output.chomp
end

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

attr_reader :value

def initialize
@value = 0
@entered = []
end

def divide
operate &:/
end

def evaluate formula
formula = tokens formula
formula.each do |token|
(token.class == Symbol ? operate(&token) : push(token))
end
return @value
end

def minus
operate &:-
end

def operate &operator
raise "calculator is empty" if @entered.length < 2
@value = @entered.push(@entered.pop(2).reduce &operator)[-1]
end

def plus
operate &:+
end

def push num
@entered << num.to_f
end

def times
operate &:*
end

def tokens token_list
token_list.split(" ").map do |token|
(token.to_i.to_s == token ? token.to_i : token.to_sym)
end
end

end
Loading