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(name)
"Hello, #{name}!"
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(fahrenheit)
celcius = (fahrenheit - 32) * (5.0/9.0)
end

def ctof(celcius)
fahrenheit = celcius * (9.0/5.0) + 32
end
46 changes: 46 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def add(a, b)
total = a + b
end

def subtract(a, b)
total = a -b
end

def sum(array)
total = 0

array.each do |item|
total += item
end

total
end

def multiply(array)
total = 1

array.each do |item|
total *= item
end

total
end

def power(a, b)
total = 1

# total = a ** b
b.times do
total *= a
end

total
end

def factorial(a)
if a == 0 || a == 1
return 1
else
a * factorial(a - 1)
end
end
38 changes: 29 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,41 @@

describe "#multiply" do

it "multiplies two numbers"
it "multiplies two numbers" do
expect(multiply([2,2])).to eq(4)
end

it "multiplies several numbers" do
expect(multiply([2,2,3])).to eq(12)
end

it "multiplies several numbers"

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
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
29 changes: 29 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def echo(input)
"#{input}"
end

def shout(input)
"#{input.upcase}"
end

def repeat(input, number=2)
(input + " ") * (number-1) + input
end

def start_of_word(string, position)
string[0..position-1]
end

def first_word(string)
all_words = string.split(" ")
all_words[0]
end

def titleize(string)
no_capitalization = ["and", "over", "the"]

all_words = string.split(" ")
all_words.each { |word| word.capitalize! unless no_capitalization.include?(word) }
all_words[0].capitalize!
all_words.join(" ")
end
45 changes: 45 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def is_vowel?(character)
vowels = ["a", "e", "i", "o", "u"]

if vowels.include?(character)
true
else
false
end
end


def consonants(string)
result = ""
first = string[0]
second = string[1]
third = string[2]

if !is_vowel?(second) && !is_vowel?(third) || (second =="q" && third == "u")
word_begin = string[0..2]
result += string[3..-1] + word_begin + "ay"
elsif !is_vowel?(second) || (first =="q" && second == "u")
word_begin = string[0..1]
result += string[2..-1] + word_begin + "ay"
else
result += string[1..-1] + first + "ay"
end
end


def translate(string)
split_words = string.split(" ")
result = ""

if split_words.length > 1
translated = split_words.collect { |word| translate(word)}
return translated.join(" ")
end

if is_vowel?(string[0])
result += "#{string}ay"
else
consonants(string)
end

end
18 changes: 18 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def reverser
result = []

yield.split.each do |word|
result << word.reverse
end

result.join(" ")
end

def adder(added_num=1)
result = yield
result += added_num
end

def repeater(num=1)
num.times { |item| yield }
end
9 changes: 9 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def measure(num=1)
first_time = Time.now

num.times { |item| yield }

second_time = Time.now

elapsed_time = (second_time - first_time)/num
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
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
def title
@title
end

def title=(string)
@title = titlelize(string)
end

def titlelize(string)
no_capitalization = ["a", "an", "and", "in", "over", "of", "the"]

all_words = string.split(" ")
all_words.each { |word| word.capitalize! unless no_capitalization.include?(word) }
all_words[0].capitalize!
all_words.join(" ")
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
attr_accessor :seconds

def initialize
@seconds = 0
end

def time_string
the_time = ""

secs = @seconds % 60
mins = (@seconds/60) % 60
hrs = @seconds/3600

the_time = "#{format(hrs)}:#{format(mins)}:#{format(secs)}"
end

def format(input)
input.to_s.length > 1 ? input.to_s : "0#{input.to_s}"
end

end
37 changes: 37 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Temperature
def initialize(options={})
@fahrenheit = options[:f]
@celsius = options[:c]

@fahrenheit = in_fahrenheit if options[:c]
@celsius = in_celsius if options[:f]
end

def in_fahrenheit
@fahrenheit = @celsius * (9.0/5.0) + 32
end

def in_celsius
@celsius = (@fahrenheit - 32) * (5.0/9.0)
end

def self.from_celsius(celsius)
Temperature.new(c: celsius)
end

def self.from_fahrenheit(fahrenheit)
Temperature.new(f: fahrenheit)
end
end

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

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

def initialize
@entries = {}
end

def add(listing)
if listing.is_a?(String)
@entries[listing] = nil
else
listing.each do |key, value|
@entries[key] = value
end
end
end

def keywords
@entries.keys.sort
end

def include?(listing)
@entries.include?(listing)
end

def find(input)
matches = {}
@entries.each do |key, value|
if key[0..input.length-1] == input
matches[key] = value
end
end

matches
end

def printable
print = []

@entries.sort.map do |key, value|
print << "[#{key}] \"#{value}\""
end

print.join("\n")
end

end
Loading