Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created July 22, 2012 20:15
Show Gist options
  • Save dmalikov/3160931 to your computer and use it in GitHub Desktop.
Save dmalikov/3160931 to your computer and use it in GitHub Desktop.
SAAS homework 1
def palindrome? (string)
filtered = string.downcase.gsub(/\W/,'')
filtered.reverse == filtered
end
def count_words (sentence)
words = sentence.downcase.split(/\W/)
result = {}
words.uniq.select{ |w| !w.empty? }.each do |e|
result.store(e, words.count(e))
end
return result
end
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
class IncorrectTournamentData < StandardError ; end
def rps_game_winner( game )
raise WrongNumberOfPlayersError unless game.length == 2
raise NoSuchStrategyError unless game.all? do |e|
["R","P","S"].include?(e[1])
end
player1, player2 = game[0], game[1]
s1, s2 = player1[1], player2[1]
if [["R", "S"], ["S", "P"], ["P", "R"]].include?([s2,s1])
return player2
else
return player1
end
end
def rps_tournament_winner (games)
case games[0][0]
when String
rps_game_winner games
when Array
rps_game_winner games.map{|g| rps_tournament_winner g}
else
raise IncorrectTournamentData
end
end
class String
def anagram?( word )
def sort_( a ) ; a.downcase.chars.sort.join ; end
sort_(self) == sort_(word)
end
end
def combine_anagrams( words )
words.map { |w| words.select{ |x| x.anagram? w }.sort }.uniq
end
class Dessert
def initialize(name, calories)
@name, @calories = name, calories
end
attr_accessor :name
attr_accessor :calories
def healthy?
@calories < 200
end
def delicious?
true
end
end
class JellyBean < Dessert
def initialize(name, calories, flavor)
@name, @calories, @flavor = name, calories, flavor
end
attr_accessor :flavor
def delicious?
@flavor != "black licorice"
end
end
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s # make sure it's a string
attr_reader attr_name
attr_reader attr_name+"_history"
class_eval %Q{
def #{attr_name}_history
@#{attr_name}_history || [nil]
end
def #{attr_name}=(new_value)
@#{attr_name}_history ||= [nil]
@#{attr_name}_history << @#{attr_name} = new_value
end
}
end
end
# metaprogramming to the rescue!
class Numeric
@@currencies = {
'yen' => 0.013,
'euro' => 1.292,
'rupee' => 0.019,
'dollar' => 1.0
}
def to_singular (currency)
currency.to_s.gsub(/s$/, '')
end
def method_missing(method_id)
singular_currency = to_singular(method_id)
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
end
end
def in(currency)
self / @@currencies[to_singular(currency)]
end
end
class String
def palindrome?
filtered = self.downcase.gsub(/\W/,'')
filtered.reverse == filtered
end
end
module Enumerable
def palindrome?
self.reverse_each.zip(self).all? { |a,b| a == b }
end
end
class CartesianProduct
include Enumerable
def initialize (a, b)
@a, @b = a, b
end
def each
@a.each{ |x| @b.each { |y| yield [x,y] } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment