# Homework 1 - Part 1 def palindrome?(string) string = string.downcase.gsub(/\W/, '') string == string.reverse end def count_words(string) Hash[string.downcase.scan(/\w+/).group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}] end # Homework 1 - 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 # Homework 1 - Part 3 def combine_anagrams(words) words.group_by{|w| w.downcase.chars.sort.to_s}.values end # Homework 1 - 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 # Homework 1 - 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