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
Binary file added .DS_Store
Binary file not shown.
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
9 changes: 9 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def ftoc(fahrenheit)
c = (fahrenheit - 32).to_f * 5/9
return c
end

def ctof(celcius)
f = (celcius.to_f * 9/5) + 32
return f
end
37 changes: 37 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def add(a,b)
a + b
end

def subtract(a,b)
a - b
end

def sum(array)
sum = 0
array.each do |i|
sum += i
end
sum
end

def multiply(array)
product = 1
array.each do |i|
product *= i
end
product
end

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

def factorial(n)
if n == 1
return 1
elsif n == 0
return 0
else
n * factorial(n - 1)
end
end
36 changes: 28 additions & 8 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,8])).to eq(16)
end

it "multiplies several numbers"
it "multiplies several numbers" do
expect(multiply([2,5,3,4])).to eq(120)
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(5,3)).to eq(125)
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(0)
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(str)
str
end

def shout(str)
str.upcase
end

def repeat(str, num = 2)
phrase = ""
until num == 0
phrase += str
phrase += " "
num -= 1
end
phrase.strip
end

def start_of_word(word,num_letters)
word.slice(0, num_letters)
end

def first_word(str)
str.split(' ').first
end

def titleize(str)
small_word = ['and','over','the']
str_array = str.split(' ')
str_array.each_with_index do |i, index|
if index == 0
i.capitalize!
elsif small_word.include?(i)
i
else
i.capitalize!
end
end
str_array.join(' ')
end
61 changes: 61 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'pry'
def translate(str)

str.downcase!
# split string into words
words = split_str(str)

# split word into letters
letters = split_words(words)

# until reach a vowel (unless qu), unshift consonants
# and push those consonants to an end_of_word array
# append the end_of_word array to the array
# append the 'ay' array to the array
pig_latinned_arrays = move_consonants(letters)

# join the letters array
# join the word array
# return the array
pig_latinned_string = join_arrays(pig_latinned_arrays)
return pig_latinned_string

end

def split_str(str)
str.split(' ')
end

def split_words(words)
letters = words.map do |word|
word.split('')
end
letters
end

def move_consonants(letters)
vowels = ['a','e','i','o','u']
ay = ['a','y']
letters.each do |word|
consonants_array = []
until vowels.include?(word[0])
if word[0] == 'q' && word[1] == 'u'
2.times do |i|
consonants_array << word.shift
end
else
consonants_array << word.shift
end
end
word.concat(consonants_array).concat(ay)
end
letters
end

def join_arrays(pig_latinned_arrays)
joined_words = []
pig_latinned_arrays.each do |word|
joined_words << word.join('')
end
joined_words.join(' ')
end
19 changes: 19 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def reverser
split_string = yield.split(' ')
split_string.map do |word|
word.reverse!
end
result = split_string.join(' ')
result
end

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

def repeater(num = 1)
num.times do |i|
yield
end
end
8 changes: 8 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def measure(num_loops = 1)
start_time = Time.now
num_loops.times do
yield
end
end_time = Time.now
(end_time - start_time) / num_loops
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
21 changes: 21 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Book

def title=(title)
lowercase_words = ['the','a','an','and','in','of']
word_array = title.split(' ')
word_array.map do |word|
if lowercase_words.include?(word)
word
else
word.capitalize!
end
end
word_array[0].capitalize!
@title = word_array.join(' ')
end

def title
@title
end

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

# what we're trying to do here is create an object "Timer" with a seconds method that takes the number of seconds elapsed, and a time_string method that returns in hour: minute: second format

require 'pry'

class Timer

def initialize(s = 0, m = 0, h = 0)
@seconds = s
@minutes = m
@hours = h
end

def seconds
@seconds
end

def seconds=(number)
@seconds = number
end

def time_string
calculate_clock_time
make_time_read_friendly
end

end

def calculate_clock_time
if @seconds > 60
@minutes = @seconds / 60
@seconds -= @minutes * 60
end
if @minutes > 60
@hours = @minutes / 60
@minutes -= @hours * 60
end
end

def make_time_read_friendly
time_array = [@hours.to_s,@minutes.to_s,@seconds.to_s]
time_array.map do |time|
time.prepend('0') until time.length == 2
end
time_array.join(':')
end
45 changes: 45 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'pry'
class Temperature

def initialize(temperatures = {:f => nil, :c => nil })
@fahrenheit = temperatures[:f]
@celsius = temperatures[:c]
end

def in_fahrenheit
if @fahrenheit
@fahrenheit
else
(@celsius.to_f * 9/5).to_f + 32.to_f
end
end

def in_celsius
if @celsius
@celsius
else
(@fahrenheit.to_f - 32).to_f * 5/9.to_f
end
end

def self.from_celsius(c)
self.new(:c => c)
end

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

end

class Celsius < Temperature
def initialize(c)
@celsius = c
end
end

class Fahrenheit < Temperature
def initialize(f)
@fahrenheit = f
end
end
Loading