Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmittakarin8/1981067 to your computer and use it in GitHub Desktop.
Save dmittakarin8/1981067 to your computer and use it in GitHub Desktop.
# Part 1
def palindrome?(string)
string = string.downcase.gsub(/\W/, '')
string == string.reverse
end
def count_words(string)
Hash[string.downcase.gsub(/[^\w\s]/,'').split(' ').group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}]
end
# Part 2
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def lose?(mine, his)
mine.upcase!; his.upcase!
strategies = ['R', 'P', 'S']
if(!strategies.include?(mine) or !strategies.include?(his))
raise NoSuchStrategyError
end
["RP", "PS", "SR"].include?(mine + his)
end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
if(lose?(game[0][1], game[1][1]))
game[1]
else
game[0]
end
end
def rps_tournament_winner(game)
if(game[0][0].kind_of?(String))
rps_game_winner(game)
else
rps_game_winner [rps_tournament_winner(game[0]), rps_tournament_winner(game[1])]
end
end
# Part 3
def combine_anagrams(words)
words.group_by{|w| w.downcase.chars.sort.to_s}.values
end
# Part 4
class Dessert
def initialize(name, calories)
@name = name
@calories = calories
end
attr_accessor :name
attr_accessor :calories
def healthy?
@calories < 200
end
def delicious?
true
end
end
class JellyBean < Dessert
attr_accessor :flavor
def initialize(name, calories, flavor)
super(name, calories)
@flavor = flavor
end
def delicious?
@flavor != "black licorice"
end
end
# Part 5
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
attr_reader attr_name
attr_reader attr_name+"_history"
class_eval %Q{
def #{attr_name}=(value)
if(!defined?(@#{attr_name}_history))
@#{attr_name}_history = [@#{attr_name}]
end
@#{attr_name} = value
@#{attr_name}_history << value
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment