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

def greet(who)
"Hello, #{who}!"
"Hello, #{who}!"
end
4 changes: 2 additions & 2 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#write your code here
def ftoc(temp)
c = (temp - 32) * (5.0 / 9.0)
c = (temp - 32) * (5.0 / 9.0)
end

def ctof(temp)
f = temp * (9.0 / 5.0) + 32
f = temp * (9.0 / 5.0) + 32
end
26 changes: 13 additions & 13 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#write your code here
def add(a, b)
a + b
a + b
end

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

def sum(array)
array.inject(0) do |sum, item|
sum += item
end
array.inject(0) do |sum, item|
sum += item
end
end

def multiply(*num)
total = num.inject(1, :*)
total = num.inject(1, :*)
end

def power(a, b)
a**b
a**b
end

def factorial(a)
fact = 1
while a > 0
fact *= a
a -= 1
end
return fact
fact = 1
while a > 0
fact *= a
a -= 1
end
return fact
end
56 changes: 26 additions & 30 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
#write your code here
def echo(string)
return string
string
end

def shout(string)
return string.upcase
string.upcase
end


# def repeat(string)
# return "#{string} #{string}"
# end

def repeat(string, num = 2)
repeating = Array.new(num, string).join(" ")
return repeating
Array.new(num, string).join(" ")
end

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

def first_word(string)
i = 0
word = ""
while string[i] != " " && i < string.length
word += string[i]
i += 1
end
return word
i = 0
word = ""
while string[i] != " " && i < string.length
word += string[i]
i += 1
end
return word
end


#split into an array on the spaces
#capitalize each index in array
#combine array indices into string
Expand All @@ -41,23 +38,22 @@ def first_word(string)
#There's got to be a better way to do this, but it works

def titleize(string)
array = string.split
array2 = []
i = 0

while i < array.length
if i == 0
array2 << array[i].capitalize
elsif i > 0 && array[i] != "and" && array[i] != "as" && array[i] != "but" && array[i] != "for" && array[i] != "if" && array[i] != "nor" && array[i] != "or" && array[i] != "so" && array[i] != "yet" && array[i] != "a" && array[i] != "an" && array[i] != "the" && array[i] != "as" && array[i] != "at" && array[i] != "by" && array[i] != "for" && array[i] != "in" && array[i] != "of" && array[i] != "off" && array[i] != "on" && array[i] != "per" && array[i] != "to" && array[i] != "up" && array[i] != "via" && array[i] != "over"
array2 << array[i].capitalize
else
array2 << array[i]
end
i += 1
array = string.split
translated_word = []
i = 0
little_words=["a", "an", "over", "the", "and", "in", "of"]

while i < array.length
if i == 0
translated_word << array[i].capitalize
elsif little_words.include?(array[i])
translated_word << array[i]
else
translated_word << array[i].capitalize
end
result = array2.join(" ")
i += 1
end
result = translated_word.join(" ")
return result
end



37 changes: 17 additions & 20 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@
#if array 0-3 include sch or qu or ch

def translate(string)
array = string.split
array2 = []
array = string.split
translated_word = []

array.each do |word|
letter_u = word.index("u")
array.each do |word|
letter_u = word.index("u")

if "aeiou".include?(word[0])
array2 << word + "ay"

elsif word.include?("qu") == true && word.index("qu") <= 3

array2 << word[(letter_u + 1)..-1] + word[0..letter_u] + "ay"

else
vowel = word.index(/[aeiou]/)
array2 << word[vowel..-1] + word[0..(vowel - 1)] + "ay"
end
if "aeiou".include?(word[0])
translated_word << word + "ay"
elsif word.include?("qu") == true && word.index("qu") <= 3
translated_word << word[(letter_u + 1)..-1] + word[0..letter_u] + "ay"
else
vowel = word.index(/[aeiou]/)
translated_word << word[vowel..-1] + word[0..(vowel - 1)] + "ay"
end
end

array.each_with_index do |word, i|
if word == word.capitalize
array2[i] = array2[i].capitalize
end
array.each_with_index do |word, i|
if word == word.capitalize
translated_word[i] = translated_word[i].capitalize
end
end

return array2.join(" ")
return translated_word.join(" ")
end
97 changes: 28 additions & 69 deletions 05_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -1,77 +1,36 @@
class Book
# write your code here
# attr_writer :title
# attr_writer :title

# def initialize
# @title = title
# end
# def initialize
# @title = title
# end

def title
titleize
end
def title
titleize
end

def title=(title)
@title = title
end
def title=(title)
@title = title
end

def titleize
array = @title.split
array2 = []
i = 0
def titleize
array = @title.split
array2 = []
i = 0
little_words=["a", "an", "over", "the", "and", "in", "of"]

while i < array.length
if i == 0
array2 << array[i].capitalize
elsif i > 0 && array[i] != "and" && array[i] != "as" && array[i] != "but" && array[i] != "for" && array[i] != "if" && array[i] != "nor" && array[i] != "or" && array[i] != "so" && array[i] != "yet" && array[i] != "a" && array[i] != "an" && array[i] != "the" && array[i] != "as" && array[i] != "at" && array[i] != "by" && array[i] != "for" && array[i] != "in" && array[i] != "of" && array[i] != "off" && array[i] != "on" && array[i] != "per" && array[i] != "to" && array[i] != "up" && array[i] != "via" && array[i] != "over"
array2 << array[i].capitalize
else
array2 << array[i]
end
i += 1
while i < array.length
if i == 0
array2 << array[i].capitalize
elsif little_words.include?(array[i])
array2 << array[i]
else
array2 << array[i].capitalize
end
result = array2.join(" ")
return result
end
end


# class Name
# attr_reader :title, :first_name, :middle_name, :last_name

# def initialize(title, first_name, middle_name, last_name)
# @title = title
# @first_name = first_name
# @middle_name = middle_name
# @last_name = last_name
# end
# end

# -If we use attr_writer, we don't have to do this:
# def title=(new_title)
# @title = new_title
# end

# -Instead:
# attr_writer :title

# attr_accessor -Can use to replace attr_reader and attr_writer together

# From capitalize problem
# def titleize(string)
# array = string.split
# array2 = []
# i = 0

# while i < array.length
# if i == 0
# array2 << array[i].capitalize
# elsif i > 0 && array[i] != "and" && array[i] != "as" && array[i] != "but" && array[i] != "for" && array[i] != "if" && array[i] != "nor" && array[i] != "or" && array[i] != "so" && array[i] != "yet" && array[i] != "a" && array[i] != "an" && array[i] != "the" && array[i] != "as" && array[i] != "at" && array[i] != "by" && array[i] != "for" && array[i] != "in" && array[i] != "of" && array[i] != "off" && array[i] != "on" && array[i] != "per" && array[i] != "to" && array[i] != "up" && array[i] != "via" && array[i] != "over"
# array2 << array[i].capitalize
# else
# array2 << array[i]
# end
# i += 1
# end
# result = array2.join(" ")
# return result
# end
i += 1
end
result = array2.join(" ")
return result
end
end
4 changes: 0 additions & 4 deletions 06_timer/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def time_string
# if min > 59, hour + 1 and min - 60 until min < 59 (while min > 59
min = 0
hour = 0


# return seconds if it's less than 1 minute
if min == 0 && @sec <= 59
Expand All @@ -42,12 +41,9 @@ def time_string
hour += 1
min -= 60
end


return "#{padded(hour)}:#{padded(min)}:#{padded(@sec)}"
end


def padded(time)
# if integer < 9, add 0 to front and return both
# if integer > 9, return integer
Expand Down