Created
March 7, 2012 21:03
-
-
Save andrewdavidcostello/1996238 to your computer and use it in GitHub Desktop.
SaaS-Class: Homework 1
This file contains hidden or 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
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 # create the attribute's getter | |
attr_reader attr_name + "_history" #create history getter | |
class_eval %{ | |
def #{attr_name}=(val) | |
@#{attr_name} = val | |
@#{attr_name}_history = [nil] if @#{attr_name}_history.nil? | |
@#{attr_name}_history.push(val) | |
end | |
} | |
end | |
end | |
class Foo | |
attr_accessor_with_history :bar | |
end | |
f = Foo.new | |
f.bar = 1 | |
f.bar = 2 | |
f = Foo.new | |
f.bar = 4 | |
p f.bar_history |
This file contains hidden or 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
class Anagram | |
def self.combine(words) | |
anagrams = words.uniq.inject Hash.new [] do |anagrams, word| | |
group = word.chars.sort.join | |
anagrams[group] += [word] if group == word.chars.sort.join | |
anagrams | |
end | |
anagrams.values #Output as array | |
end | |
end | |
p Anagram.combine(['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream']) |
This file contains hidden or 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
class Dessert | |
attr_accessor :name | |
attr_accessor :calories | |
def initialize(name, calories) | |
@name = name | |
@calories = calories | |
end | |
def healthy? | |
@calories < 200 | |
end | |
def delicious? | |
true | |
end | |
end | |
class JellyBean < Dessert | |
attr_accessor :flavor | |
def initialize(name, calories, flavor) | |
@name = name | |
@calories = calories | |
@flavor = flavor | |
end | |
def delicious? | |
@flavor == 'black licorice' ? false : true | |
end | |
end | |
dessert = Dessert.new('cheesecake',250) | |
p dessert.delicious? | |
p dessert.healthy? | |
bean = JellyBean.new('bertiebots',150,'cinnamon') | |
p bean.delicious? | |
p bean.healthy? |
This file contains hidden or 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
class String | |
def palindrome? | |
self.gsub!(/\W+/, '').downcase! | |
self == self.reverse | |
end | |
def word_count | |
downcase.split.inject Hash.new(0) do |count, word| | |
count[word] +=1 | |
count | |
end | |
end | |
end | |
print "A man, a plan, a canal -- Panama".palindrome? | |
print "Doo bee doo bee doo".word_count |
This file contains hidden or 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
class WrongNumberOfPlayersError < StandardError ; end | |
class NoSuchStrategyError < StandardError ; end | |
class RockPaperScissors | |
def rps_game_winner(game) | |
raise WrongNumberOfPlayersError unless game.length == 2 | |
game.each do |player, strategy| | |
raise NoSuchStrategyError unless ['R','P','S'].include?(strategy.upcase) | |
end | |
if compare?(game) | |
puts game[0][0] + " Wins!" | |
game[0] | |
else | |
puts game[1][0] + " Wins!" | |
game[1] | |
end | |
end | |
def rps_tournament_winner(game) | |
if game[0][1].class==String | |
rps_game_winner(game) | |
else | |
branch1=rps_tournament_winner(game[0]) | |
branch2=rps_tournament_winner(game[1]) | |
rps_tournament_winner([branch1,branch2]) | |
end | |
end | |
def compare?(game) | |
(game[0][1] + game[1][1]) =~ /rs|sp|pr|rr|ss|pp/i | |
end | |
end | |
game = RockPaperScissors.new | |
list = [ [ "Armando", "P" ], [ "Dave", "S" ] ] | |
game.rps_game_winner(list) | |
tournament = [ [ | |
[ [ "Armando", "P" ], [ "Dave", "S" ] ], | |
[ [ "Richard", "R"], ["Michael", "S"] ], | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
] ] | |
game.rps_tournament_winner(tournament) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment