Skip to content
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

RSPEC Practice Exercises #96

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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 @@
#convert fahrenheit to celsius
def ftoc(temp_f)
(temp_f - 32) * (5.0/9.0)
end

#convert celsius to fahrenheit
def ctof(temp_c)
(temp_c) * (9.0/5.0) + 32
end
32 changes: 32 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def add(num_1, num_2)
num_1 + num_2
end

def subtract(num_1, num_2)
num_1 - num_2
end

def sum(array)
sum = 0
array.each {|num| sum += num}
sum
end

def multiply(*args)
product = 1
args.each {|num| product *= num}
product
end

def power(base, exponent)
result = 1
exponent.times {result*=base}
result
end

def factorial(num)
return 1 if num == 0 #by definition
result = 1
(1..num).each {|x| result *= x}
return result
end
34 changes: 25 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,37 @@

describe "#multiply" do

it "multiplies two numbers"
it "multiplies two numbers" do
expect(multiply(10,4)).to eq(40)
end

it "multiplies several numbers" do
expect(multiply(2,3,4)).to eq(24)
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(3,2)).to eq(9)
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
38 changes: 38 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def echo(str)
str
end

def shout(str)
str.upcase
end

def repeat(str, num=2)
result = (str + ' ') * num
result[0..-2] #remove trailing space
end

def start_of_word(str, num=1)
str[0..num-1]
end

def first_word(str)
str.split(' ')[0]
end


def titleize(str)
result = str.split(' ').each_with_index do |word, index|
if index == 0
word.capitalize!
elsif (word != 'and') && (word != 'over') && (word != 'the')
word.capitalize!
end

end
result.join(' ')
end

# # p first_word('hello there')
# z = ''
# z= 'david copperfield'.split(' ').each {|x| x.capitalize!}
# p z.join(' ')
39 changes: 39 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def translate (words)
result = words.split(' ').each do |word|

#check for 'qu'
if word.index('qu')
word.insert(-1, word[0..word.index('qu')+1])
word.slice!(0..word.index('qu')+1)
word << 'ay'
next

#check if first 3 letters are consonants
elsif word[0] =~ (/[^aeiou]/) && word[1] =~ (/[^aeiou]/) && word[2] =~ (/[^aeiou]/)
word.insert(-1, word[0..2])
word.slice!(0..2)
word << 'ay'
next

#check if first 2 letters are consonants or word starts with 'qu'
elsif word[0] =~ (/[^aeiou]/) && word[1] =~ (/[^aeiou]/)
word.insert(-1, word[0..1])
word.slice!(0..1)
word << 'ay'
next

#check if first letter is a consonant
elsif word[0] =~ (/[^aeiou]/)
word.insert(-1, word[0])
word.slice!(0)
word << 'ay'
next
#first letter will be a vowel
else
word << 'ay'
end
end
result.join(' ')
end

# p translate('apple pie')
16 changes: 16 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def reverser

yield.split(' ').each {|word| word.to_s.reverse!}.join(' ')

end

def adder(num=1)
yield + num
end


def repeater(numtimes=1)

numtimes.times {yield}

end
11 changes: 11 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "time"

def measure(num_times=1)

start_time = Time.now
num_times.times {yield}
(Time.now-start_time)/num_times #average time

end

puts Time.now()
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
"Hello, #{who}!"
else
"Hello!"
end
end
end
19 changes: 19 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Book
attr_reader :title

def title=(title)
#words to not capitalize
wordslist = ["and", "over", "the", "a", "in", "of", "an"]

@title = title
result= @title.split(' ').each_with_index do |word, index|
if index == 0
word.capitalize!
elsif wordslist.index(word) == nil
word.capitalize!
end
end
@title = result.join(' ')
end

end
20 changes: 20 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Timer
def initialize
@seconds = 0
end

attr_reader :seconds

def seconds=(seconds)
@seconds = seconds
end

def time_string
hours = @seconds / 3600
mins = (@seconds % 3600) / 60
secs = (@seconds % 3600) % 60
format("%02d:%02d:%02d", hours, mins, secs)

end

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 @@
class Temperature


def initialize(temp = {})
@temp = temp

end

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

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

def in_fahrenheit
if @temp[:f]
@temp[:f]
elsif @temp[:c]
(@temp[:c]) * (9.0/5.0) + 32
end
end

def in_celsius
if @temp[:c]
@temp[:c]
elsif @temp[:f]
(@temp[:f] - 32) * (5.0/9.0)
end
end
end

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

class Fahrenheit < Temperature
def initialize(temp)
super(:f => temp)
end

end
49 changes: 49 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Dictionary

def initialize
@dic = {}
end

def entries
@dic
end

def add(entry)
if entry.is_a?(Hash)
@dic.merge!(entry)
#add key only
elsif entry.is_a?(String)
@dic.merge!({entry => nil})
end
end

def keywords
@dic.keys.sort!
end

def include?(key)
@dic.key?(key)
end

def find(string)
results = {}
@dic.each do |key,val|
if key.index(string)
#if the key contains the search string, add k-v pair to results
results.merge!({key => val})
end
end
results
end

def printable
hash = entries.sort
result = ""
hash.each do |key,val|
result << "[#{key}] \"#{val}\"\n"
end
result

end

end
Loading