Skip to content

Submit assignment #84

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 15 commits into
base: master
Choose a base branch
from
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(f)
(f.to_f - 32.0) * 5.0/9.0
end

def ctof(c)
c.to_f * 9.0/5.0 + 32
end
24 changes: 24 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def add(first, second)
first + second
end

def subtract(first, second)
first - second
end

def sum(array)
array.reduce(0) { |a, b| a + b }
end

def multiply(array)
array.reduce { |a, b| a * b }
end

def power(base, factor)
base ** factor
end

def factorial(n)
return 1 if n == 0
(1..n).to_a.reduce { |a, b| a * b }
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([2,3])).to eq(6)
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(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
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(text)
text
end

def shout(text)
text.upcase
end

def repeat(text, times=2)
text + ((" "+text) * (times-1))
end

def start_of_word(text, letters)
text[0..letters-1]
end

def first_word(text)
array = text.split(' ')
array[0]
end

def capitalize(word)
word[0] = word[0].upcase
word
end

def titleize(text)
array = text.split(' ')
little_words = ['a', 'an', 'the', 'and', 'or', 'but', 'for', 'by', 'near', 'under', 'over', 'of']
output = array.map.with_index(0) do |word, index|
if index == 0
capitalize(word)
elsif little_words.include?(word)
word
else
capitalize(word)
end
end
output = output.join(' ')
end
28 changes: 28 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def translate_word(text)
vowels = ['a', 'e', 'i', 'o', 'u', 'y']
if vowels.include?(text[0])
output = text + 'ay'
else
array = text.split('')
array.length.times do
current_letter = array.shift
if vowels.include?(current_letter)
array.unshift(current_letter)
break
elsif current_letter == 'q'
current_letter = current_letter + array.shift
array.push(current_letter)
else
array.push(current_letter)
end
end
output = array.join('') + 'ay'
end
output
end

def translate(text)
words = text.split(' ')
output = words.map { |word| translate_word(word) }
output.join(' ')
end
22 changes: 22 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def word_reverser
input = yield.split('')
output = []
while input.length > 0
output.push(input.pop)
end
output.join('')
end

def reverser
words = yield.split(' ')
output = words.map { |word| word_reverser { word } }
output.join(' ')
end

def adder(addend=1)
yield + addend
end

def repeater(multiplicand=1)
multiplicand.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 @@
def measure(count=1)
recorded_times = []
count.times do
start = Time.now
yield
finish = Time.now
recorded_times.push(finish - start)
end
sum_of_times = recorded_times.reduce(0){|sum, x| sum + x}
sum_of_times / count
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(name="")
if name.length == 0
"Hello!"
else
"Hello, #{name}!"
end
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 title=(title)
@title = titleize(title)
end
def title
@title
end

def capitalize(word)
word[0] = word[0].upcase
word
end

def titleize(text)
array = text.split(' ')
little_words = ['a', 'an', 'the', 'and', 'or', 'but', 'for', 'by', 'near', 'under', 'over', 'of', 'in']
output = array.map.with_index(0) do |word, index|
if index == 0
capitalize(word)
elsif little_words.include?(word)
word
else
capitalize(word)
end
end
output = output.join(' ')
end

end
21 changes: 21 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Timer
def initialize
@seconds = 0
end
def seconds=(seconds=0)
@seconds = seconds
end
def seconds
@seconds
end
def time_string
minutes = @seconds / 60
seconds = @seconds % 60
hours = minutes / 60
minutes = minutes % 60
"#{format_time_division(hours)}:#{format_time_division(minutes)}:#{format_time_division(seconds)}"
end
def format_time_division(number)
number.to_s.rjust(2, '0')
end
end
35 changes: 35 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Temperature
def initialize(options={})
@f = options[:f] ||= options[:fahrenheit]
@c = options[:c] ||= options[:celsius]
end
def self.ftoc(f)
(f.to_f - 32.0) * 5.0/9.0
end
def self.ctof(c)
c.to_f * 9.0/5.0 + 32
end
def in_fahrenheit
@f || self.class.ctof(@c)
end
def in_celsius
@c || self.class.ftoc(@f)
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(c)
@c = c
end
end
class Fahrenheit < Temperature
def initialize(f)
@f = f
end
end
7 changes: 6 additions & 1 deletion 10_temperature_object/temperature_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@
# run *all* the tests during your refactoring, to make sure you did it right
#
describe "utility class methods" do

it "ftoc method" do
expect(Temperature.ftoc(32)).to eq(0.0)
end
it "ctof method" do
expect(Temperature.ftoc(212)).to eq(100.0)
end
end

# Here's another way to solve the problem!
Expand Down
37 changes: 37 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Dictionary
def initialize
@dict = {}
@keywords = []
end
def entries
@dict
end
def add(pair)
if pair.class == Hash
@dict.merge!(pair)
@keywords.push(pair.keys[0])
elsif
@dict.merge!(pair => nil)
@keywords.push(pair)
end
end
def keywords
@keywords.sort
end
def include?(query)
@keywords.include?(query)
end
def find(query)
@dict.select { |key, value| key.start_with? query}
end
def printable
@keywords.sort!
output = ""
@keywords.each do |keyword|
output += "[#{keyword}] \"#{@dict[keyword]}\"\n"
end
output = output.slice(0, output.length-1) # remove trailing \n
end
end

# ""
33 changes: 33 additions & 0 deletions 12_rpn_calculator/rpn_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class RPNCalculator
def initialize
@stack = []
end
def push(n)
@stack.push(n.to_f)
end
def value
@stack[-1]
end
def operation
begin
a = @stack.pop
b = @stack.pop
yield(a,b)
rescue
raise Exception, "calculator is empty"
end
end
def plus
operation { |a,b| @stack.push(a + b) }
end
def minus
operation { |a,b| @stack.push(b - a) }
end
def times
operation { |a,b| @stack.push(a * b) }
end
def divide
operation { |a,b| @stack.push(b / a) }
end

end
Loading