This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def oxford_comma(array) | |
if array.length == 1 | |
return "#{array[0]}" | |
elsif array.length == 2 | |
return array.join(" and ") | |
elsif array.length >= 3 | |
new_last_array_item = "and #{array[-1]}" | |
array.pop | |
array.push(new_last_array_item) | |
return array.join(", ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def oxford_comma(array) | |
if array.length == 1 | |
return "#{array[0]}" | |
elsif array.length == 2 | |
return array.join(" and ") | |
elsif array.length >= 3 | |
array[-1] = "and #{array[-1]}" | |
return array.join(", ") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FIRST METHOD THAT PASSED | |
def reverse_each_word(array) | |
new_array = array.split(" ") | |
reversed_array = new_array.each {|x| x.reverse!} | |
return reversed_array.join(" ") | |
end | |
# FIRST REFACTORED CODE | |
def reverse_each_word(array) | |
new_array = array.split(" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime?(n) | |
if n <= 1 | |
return false | |
elsif n <= 3 | |
return true | |
else (2..n/2).none? do |x| | |
n % x == 0 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def swap_elements_from_to(array, index, new_index) | |
array[index], array[new_index] = array[new_index], array[index] | |
return array | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question 4 Bonus | |
describe 'swap_elements_from_to' do | |
it 'swaps elements and allows you to specify the index of the element you would like to move to a new index' do | |
expect(swap_elements_from_to(["one", "two", "three"], 2, 1)).to eq(["one", "three", "two"]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NoMethodError: | |
undefined method `starts_with?' for "apple":String | |
Did you mean? start_with? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# given that holiday_hash looks like this: | |
# { | |
# :winter => { | |
# :christmas => ["Lights", "Wreath"], | |
# :new_years => ["Party Hats"] | |
# }, | |
# :summer => { | |
# :fourth_of_july => ["Fireworks", "BBQ"] | |
# }, | |
# :fall => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def all_holidays_with_bbq(holiday_hash) | |
holiday_hash.map do |season, holidays| | |
holidays.map do |holiday_name, supplies | |
supplies.include?("BBQ") ? holiday_name : nil | |
end | |
end.flatten.compact | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Players | |
class Computer < Player | |
def move(board) | |
possible_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] | |
valid_moves = [] | |
possible_moves.each do |move| | |
valid_moves << move if board.cells[move.to_i-1] = " " | |
end | |
valid_moves.sample | |
end |