Skip to content

Learn Ruby Exercises - Completed #77

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
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
2 changes: 2 additions & 0 deletions 00_hello/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
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
89 changes: 89 additions & 0 deletions 00_hello/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true

# Limits the available syntax to the non-monkey patched syntax that is recommended.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end

# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
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 @@
# fahrenheit to celsius
def ftoc(f)
5.0/9.0 * (f - 32)
end

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

# subtact
def subtract(a,b)
a - b
end

# sum
def sum(arr)
return 0 if arr.length == 0
return arr[0] if arr.length == 1
arr.reduce { |prev, curr| prev + curr }
end

# multiply
def mult(*args)
return false if args.length < 2
args.reduce { |prev, curr| prev * curr }
end

# power
def pow(a,b)
return 1 if b == 0
result = a
(b.abs-1).times do
result *= a
end
b > 0 ? result : 1.0/result
end

# factorial
def fac(num)
return 1 if num < 2
num * fac(num-1)
end
73 changes: 60 additions & 13 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,70 @@
# once the above tests pass,
# write tests and code for the following:

describe "#multiply" do
# describe "#multiply" do
# it "multiplies two numbers"
# it "multiplies several numbers"
# end

it "multiplies two numbers"
describe "multiply" do
it "does not multiply 0 numbers" do
expect(mult()).to eq(false)
end

it "does not multiply 1 number" do
expect(mult(2)).to eq(false)
end

it "multiplies 2 and 3" do
expect(mult(2,3)).to eq(6)
end

it "multiplies several numbers"

it "multiplies 8, 12, 16, 2, and 11" do
expect(mult(8,12,16,2,11)).to eq(33792)
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"
# end

describe "power" do
it "raises one number to the power of a positive number" do
expect(pow(3,2)).to eq(9)
end

it "raises one number to the power of a negative number" do
expect(pow(2,-4)).to eq(0.0625)
end

it "raises one number to the power of 0" do
expect(pow(1000000,0)).to eq(1)
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"
end
# 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"
# end

describe "factorial" do
it "computes the factorial of 0" do
expect(fac(0)).to eq(1)
end
it "computes the factorial of 1" do
expect(fac(1)).to eq(1)
end
it "computes the factorial of 2" do
expect(fac(2)).to eq(2)
end
it "computes the factorial of 5" do
expect(fac(5)).to eq(120)
end
it "computes the factorial of 10" do
expect(fac(10)).to eq(3628800)
end
end
45 changes: 45 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Simon Says

# Echo
def echo(word)
word
end

# Shout
def shout(word)
word.upcase
end

# Repeat
def repeat(word, times_said=2)
phrase = word
(times_said-1).times do
phrase += " " + word
end
phrase
end

# Start of word
def start_of_word(word, location)
word[0...location] # 0 to location-1
end

# First word
def first_word(phrase)
phrase_arr = phrase.split(" ")
phrase_arr[0]
end

# Capitalize
def titleize(phrase)
little_words = ["the", "a", "and", "over"]
phrase_arr = phrase.split(" ")
phrase_arr = phrase_arr.each_with_index do |word, location|
if location == 0 || !little_words.include?(word.downcase)
word[0] = word[0].upcase
end
end
phrase_arr.join(" ")
end


44 changes: 44 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Pig Latin

# Translate
def translate(phrase)

# get all vowels in an array
vowels = ['a','e','i','o','u']

# break up phrase into array
phrase_array = phrase.split(" ")

# initialize translated array
phrase_array_translated = []

# translate each word
phrase_array.each do |word|

# check if word begins with a vowel
if vowels.include?(word[0].downcase)
phrase_array_translated << word += "ay"
next

# check if word begins with a consonant or consonant "chunk"
else
consonant_chunk = ""
letter_tracker = 0

while !vowels.include?(word[letter_tracker].downcase) || # keep going until a vowel is hit, unless...
word[letter_tracker].downcase == "u" && word[letter_tracker-1].downcase == "q" # ...special case "qu" is found

consonant_chunk += word[letter_tracker]
letter_tracker += 1

end

phrase_array_translated << word[letter_tracker..word.length] + consonant_chunk + "ay"
end

end

phrase_array_translated.join(" ")

end

20 changes: 20 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Reverser
def reverser(&block)
phrase_arr = block.call.split(" ") # get content of block
phrase_arr.each do |word|
word.reverse! # reverse element on phrase_arr directly (no copies made, potentially dangerous!)
end
phrase_arr.join(" ")
end

# Adder
def adder(val=1) # block doesn't need to be mentioned
yield + val
end

# Repeater
def repeater(val=1) # again, block doesn't need to be mentioned
val.times do
yield
end
end
7 changes: 7 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Performance Monitor

def measure(val=1)
start_time = Time.now
val.times { yield }
(Time.now - start_time) / val.to_f
end
8 changes: 8 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Friend

def greeting(name = nil)
return "Hello!" if !name
"Hello, " + name + "!"
end

end
30 changes: 30 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Book

# Constructor
def initialize
@title = ""
end

# Read
def title
@title
end

# Write
def title=(phrase)
phrase_array = []
non_caps = ["and", "in", "the", "of", "a", "an"]
phrase.split(" ").each_with_index do |word, location|
if non_caps.include?(word.downcase) && location != 0
phrase_array << word
else
phrase_array << word[0].upcase + word[1..word.length]
end
end
@title = phrase_array.join(" ")
end

end



Loading