Created
March 4, 2012 15:30
-
-
Save tomtung/1973534 to your computer and use it in GitHub Desktop.
SaaS Class Homework: Ruby Basics
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
# 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 | |
# Homework 2 - 1 - a | |
class Numeric | |
@@currencies = {'dollar' => 1.0, 'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019} | |
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) | |
singular_currency = to_singular(currency) | |
if @@currencies.has_key?(singular_currency) | |
self / @@currencies[singular_currency] | |
else | |
super | |
end | |
end | |
private | |
def to_singular(word) | |
word.to_s.gsub( /s$/, '') | |
end | |
end | |
# Homework 2 - 1 - b | |
class String | |
def palindrome? | |
# Calls palindrome?(s) defined in Homework 1 | |
Object.send(:palindrome?, self) | |
end | |
end | |
# Homework 2 - 1 - c | |
module Enumerable | |
def palindrome? | |
self.collect{|x| x} == self.collect{|x| x}.reverse | |
end | |
end | |
# Homework 2 - 2 | |
class CartesianProduct | |
include Enumerable | |
attr_reader :l_enum, :r_enum | |
def initialize(l_enum, r_enum) | |
@l_enum = l_enum | |
@r_enum = r_enum | |
end | |
def each | |
self.l_enum.each { | |
|l| self.r_enum.each { | |
|r| yield [l, r] | |
} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment