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
7 changes: 7 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
#write your code here
def hello
"Hello!"
end

def greet(name)
"Hello, #{name}!"
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct.

7 changes: 7 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
#write your code here
def ftoc(temperature)
return (temperature - 32) * 5 / 9
end

def ctof(temperature)
return temperature * 9.0 / 5 + 32
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good also.

31 changes: 31 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
#write your code here
def add(num1, num2)
return num1 + num2
end

def subtract(num1, num2)
return num1 - num2
end

def sum(array)
return array.sum
end

def multiply(*nums) #ruby treats "*nums" as an array
multiply = 1
nums.each do |n|
multiply *= n
end
return multiply
end

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

def factorial(num)
result = 1
num.times do |number|
result*= (number+1)
end
return result
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

35 changes: 24 additions & 11 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,35 @@
# write tests and code for the following:

describe "#multiply" do

it "multiplies two numbers"

it "multiplies several numbers"

it "multiplies two numbers" do
expect(multiply(2,6)).to eq(12)
end
it "multiplies several numbers" do
expect(multiply(2,6,5)).to eq(60)
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(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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is properly done!

35 changes: 35 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
#write your code here
def echo(string)
return string
end

def shout(string)
return string.upcase
end

def repeat(string, num=2)
result = ""
result = (string + " ") * num
return result.strip
end

def start_of_word(string, num)
return string[0, num]
end

def first_word(string)
return string.split[0]
end

def titleize(string)
little_words= ["and", "over", "the"]
result = ""
string.split.each do |word|
if !little_words.include?(word)
result += word.capitalize + " "
else
result += word + " "
end
end
result = result[0].upcase + result[1,result.length-1]
return result.strip
end
55 changes: 55 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
#write your code here
def translate(string)
result = []
vowels = ["a", "e", "i", "o", "u"]

string.split.each do |word|
word_copy = word
min = word.length
vowels.each do |vowel|
if word.downcase.index(vowel) != nil && word.downcase.index(vowel) <=min #find the first vowel's index
min = word.downcase.index(vowel)
end
end
temp = "" #to save the punctuation
if !("a".."z").include?(word[word.length-1]) && !("A".."Z").include?(word[word.length-1])
temp = word[word.length-1] #save the punctuation
word = word.chop #remove the punctuation
end
new_word = word[min, word.length] + word[0, min]
if new_word[0].downcase == "u" && new_word[new_word.length-1].downcase == "q" #deal with "qu" case
new_word = new_word.delete_prefix("u") + "u"
end
if ("A".."Z").include?(word_copy[0]) #capitalized words were capitalized
new_word = new_word.downcase.capitalize
end
result.push(new_word + "ay" + temp)
end
return result.join(" ")
end

# Below is the code shared by one student using regex, keep it as reference.
# vowels = ["a", "e", "i", "o", "u"]
# #vowels = 'aeiou'.split('')
# #vowels = %w[a e i o u]

# def translate (string)
# vowels = %w[a e i o u]
# words = string.split
# new_words = []

# words.each do |word|
# new_word =
# if word.match /^.*qu/
# first_part = word[/^.*qu/]
# last_part = word.delete first_part
# last_part + first_part + 'ay'

# else !vowels.include?(word[0])
# first_part = word[/[^aeiou]*/]
# last_part = word.delete first_part
# last_part + first_part + 'ay'
# end
# new_words.push new_word
# end
# new_words.join ' '
# end
10 changes: 10 additions & 0 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
# * retain the punctuation from the original phrase

it "capitalize the words that capitalized before" do
s = translate("The Quick Brown Fox")
expect(s).to eq("Ethay Ickquay Ownbray Oxfay")
end

it "retains the puctuation from the original phrase" do
s = translate("Ha! I got you!")
expect(s).to eq("Ahay! Iay otgay ouyay!")
end

end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

35 changes: 34 additions & 1 deletion 05_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
class Book
# write your code here
# write your code here
attr_accessor :title
def initialize
@title = ""
end

def title
little_words= ["and", "over", "the", "an", "a", "in", "of"]
result = ""
@title.split.each do |word|
if !little_words.include?(word)
result += word.capitalize + " "
else
result += word + " "
end
end
result = result[0].upcase + result[1,result.length-1]
title = result.strip
end
end

#Another student's code using each_with_index method
# def title
# little_words = ["and","the","a","an","in","of","in"]
# c= ""
# @title.split.each_with_index do |string, index|
# if index == 0
# c=string.capitalize
# elsif little_words.include? string
# c+= " " + string
# else c+= " " + string.capitalize
# end
# end
# return c
# end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in the title method looks like it would work, but how are you getting input (as in a string) for the method to operate on?

18 changes: 18 additions & 0 deletions 06_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
class Timer
#write your code here
attr_accessor :seconds

def initialize
@seconds = 0
end

def time_string
hours = @seconds / 60 / 60
seconds = @seconds % 60
tmp = @seconds / 60
minutes = tmp % 60

padded(hours) + ":" + padded(minutes) + ":" + padded(seconds)
end

def padded(num)
return result = num >= 10 ? num.to_s : "0#{num}"
end
end
22 changes: 11 additions & 11 deletions 06_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
# describe 'padded' do
# it 'pads zero' do
# expect(@timer.padded(0)).to eq('00')
# end
# it 'pads one' do
# expect(@timer.padded(1)).to eq('01')
# end
# it "doesn't pad a two-digit number" do
# expect(@timer.padded(12)).to eq('12')
# end
# end
describe 'padded' do
it 'pads zero' do
expect(@timer.padded(0)).to eq('00')
end
it 'pads one' do
expect(@timer.padded(1)).to eq('01')
end
it "doesn't pad a two-digit number" do
expect(@timer.padded(12)).to eq('12')
end
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good.


end
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rake (10.5.0)
rake (12.3.3)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
Expand All @@ -21,8 +21,8 @@ PLATFORMS
ruby

DEPENDENCIES
rake (< 11.0)
rake (~> 12.3)
rspec (~> 3.4)

BUNDLED WITH
1.13.6
1.17.3