Skip to content

Test First Ruby Exercises #95

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
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(temp)
(temp - 32) * 5.0 / 9.0
end

def ctof(temp)
temp * 9 / 5.0 + 32
end
27 changes: 27 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def add(num1, num2)
num1 + num2
end

def subtract(num1, num2)
num1 - num2
end

def sum(arr)
return 0 if arr.empty?
arr.inject(:+)
end

def multiply(arr)
return 0 if arr.empty?
arr.inject(:*)
end

def power(num1, pow)
num1**pow
end

def factorial(num)
return 0 if num == 0
fac = [*1..num]
fac.inject(:*)
end
37 changes: 26 additions & 11 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,38 @@
# once the above tests pass,
# write tests and code for the following:

describe "#multiply" do
describe "multiply" do

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

it "multiplies several numbers"

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

describe "#power" do
it "raises one number to the power of another number"
describe "power" do
it "raises one number to the power of another number" do
expect(power(10,2)).to eq(100)
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
Binary file added 03_simon_says/.DS_Store
Binary file not shown.
39 changes: 39 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def echo(word)
word
end

def shout(word)
word.upcase
end

def repeat(word, t=2)
result = word
(t-1).times do
result = result + " " + word
end
result
end

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

def first_word(phrase)
phrase.split(" ")[0]
end

def titleize(phrase)
result = ""
phrase.split(" ").each_with_index do |word, index|
if index == 0
result = result + word.capitalize + " "
else
if word == "and" || word == "the" || word == "over"
result = result + word + " "
else
result = result + word.capitalize + " "
end
end
end
result.strip
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 translate(words)
vowels = ["a", "e", "i", "o", "u"]
result = ""
words.split(" ").each do |word|
if vowels.include?(word[0])
result += word + "ay" + " "
else
while !vowels.include?(word[0])
if word.slice(0, 2) == "qu"
word = word.slice(2, word.length) + word.slice(0, 2)
else
word = word.slice(1, word.length) + word[0]
end
end
result += word + "ay" + " "
end
end
result.strip
end
14 changes: 14 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def reverser
result = yield.split(" ").map { |word| word.reverse }
result.join(" ")
end

def adder(num = 1)
yield + num
end

def repeater(num = 1)
num.times do
yield
end
end
6 changes: 6 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def measure(num = 1)
beg_time = Time.now
num.times { yield }
finish_time = Time.now
(finish_time - beg_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
"Hello, #{who}!"
else
"Hello!"
end
end
end
12 changes: 12 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Book
def initialize
@title = ''
@untitlized_words = ['a', 'an', 'of', 'in', 'and', 'the', 'over']
end
def title=(value)
@title = value.split(' ')
end
def title
@title.map.with_index { |word, index| @untitlized_words.include?(word) && index > 0 ? word : word.capitalize }.join(' ')
end
end
14 changes: 14 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Timer
def initialize
@seconds = 0
end
def seconds
@seconds
end
def seconds=(value)
@seconds = value
end
def time_string
[@seconds / 3600, @seconds / 60 % 60, @seconds % 60].map { |t| t.to_s.rjust(2, '0') }.join(':')
end
end
31 changes: 31 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Temperature
def initialize(options = {})
@f = options[:f]
@c = options[:c]
end
def in_fahrenheit
return @f if @f
@c * 9 / 5.0 + 32
end
def in_celsius
return @c if @c
(@f - 32) * 5 / 9.0
end
def self.from_celsius(c)
Temperature.new(:c => c)
end
def self.from_fahrenheit(f)
Temperature.new(:f => f)
end
end

class Celsius < Temperature
def initialize(t)
super(:c => t)
end
end
class Fahrenheit < Temperature
def initialize(t)
super(:f => t)
end
end
33 changes: 33 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Dictionary
def initialize
@entries = {}
end
def entries
@entries
end
def add(entry)
return @entries.merge!(entry) if entry.is_a?(Hash)
@entries[entry] = nil
end
def keywords
@entries.keys.sort
end
def include?(value)
keywords.include?(value)
end
def find(word)
results = {}
@entries.each do |key, value|
results[key] = value if key.match(/#{word}/)
end
results
end
def printable
s = ''
keywords.each do |key|
s += "[#{key}] \"#{@entries[key]}\""
s += "\n" if keywords.index(key) != keywords.length - 1
end
s
end
end
66 changes: 66 additions & 0 deletions 12_rpn_calculator/rpn_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class RPNCalculator
attr_accessor :stack
attr_accessor :value
def initialize
@stack = []
@value = 0
end
def push(num)
@stack.push(num)
end
def operate
error_empty if @stack.length < 2
second = @stack.pop
first = @stack.pop
new_value = yield(second, first)
@stack.push(new_value)
@value = new_value
end
def plus
operate { |second, first| first + second }
@value
end
def minus
operate { |second, first| first - second }
@value
end
def divide
operate { |second, first| first.to_f / second.to_f }
@value
end
def times
operate { |second, first| first * second }
@value
end
def error_empty
raise "calculator is empty"
end
def tokens(string)
string.split(" ").map do |token|
if token.match(/\d/)
token.to_i
elsif ['+', '-', '*', '/'].include?(token)
token.to_sym
end
end
end
def evaluate(string)
tokens(string).each do |token|
if token.is_a?(Integer)
push(token)
else
case token
when :+
plus
when :-
minus
when :*
times
when :/
divide
end
end
end
@value
end
end
12 changes: 12 additions & 0 deletions 14_array_extensions/array_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Array
def sum
return 0 if self.empty?
self.inject(:+)
end
def square
self.map { |e| e**2 }
end
def square!
self.map! { |e| e**2 }
end
end
Loading