Created
March 31, 2013 00:05
-
-
Save ukutaht/5278908 to your computer and use it in GitHub Desktop.
BerkleyX CS169.1x Homework Solutions
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
def palindrome?(str) | |
letters = str.downcase.scan(/\w/) | |
letters == x.reverse | |
end | |
def count_words(str) | |
word_count = Hash.new(0) | |
str.downcase.scan(/\w+/).each do |word| | |
word_count[word] += 1 | |
end | |
word_count | |
end |
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
class WrongNumberOfPlayersError < StandardError ; end | |
class NoSuchStrategyError < StandardError ; end | |
def rps_result(m1, m2) | |
if m1 == "S" | |
if m2 == "P" | |
return m1 | |
elsif m2 == "R" | |
return m2 | |
elsif m2 == "S" | |
return nil | |
end | |
elsif m1 == "P" | |
if m2 == "P" | |
return nil | |
elsif m2 == "S" | |
return m2 | |
elsif m2 == "R" | |
return m1 | |
end | |
elsif m1 == "R" | |
if m2 == "R" | |
return nil | |
elsif m2 == "P" | |
return m2 | |
elsif m2 == "S" | |
return m1 | |
end | |
end | |
end | |
def rps_game_winner(game) | |
raise WrongNumberOfPlayersError unless game.length == 2 | |
strategy1 = game[0][1] | |
strategy2 = game[1][1] | |
game.each do |player| | |
raise NoSuchStrategyError if player[1] !~ /(p|s|r){1}/i | |
end | |
if rps_result(strategy1,strategy2) == strategy1 | |
return game[0] | |
elsif rps_result(strategy1,strategy2) == nil | |
return game[0] | |
elsif rps_result(strategy1,strategy2) == strategy2 | |
return game[1] | |
end | |
end | |
def rps_tournament_winner(games) | |
if games.flatten.length > 4 | |
rps_game_winner([rps_tournament_winner(games[0]), rps_tournament_winner(games[1])]) | |
else | |
rps_game_winner(games) | |
end | |
end | |
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
def combine_anagrams(words) | |
result = Hash.new() | |
words.each do |word| | |
sorted = word.downcase.chars.sort.join | |
if result.has_key?(sorted) | |
result.store(sorted,result.fetch(sorted) << word) | |
else | |
result.store(sorted, Array.new(1,word)) | |
end | |
end | |
return result.values | |
end | |
puts combine_anagrams(['cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream']) |
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
class Dessert | |
attr_accessor :name, :calories | |
def initialize(name, calories) | |
@name = name | |
@calories = calories | |
end | |
def healthy? | |
return true if @calories<200 | |
end | |
def delicious? | |
return true | |
end | |
end | |
class JellyBean < Dessert | |
attr_accessor :flavor | |
def initialize(name, calories, flavor) | |
@name = name | |
@calories = calories | |
@flavor = flavor | |
end | |
def delicious? | |
return true unless @flavor == "black licorice" | |
end | |
end |
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
class Class # http://paul-wong-jr.blogspot.com/2012/04/ruby-metaprogramming-with-classeval.html | |
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 bar_history getter | |
# write our setter code below | |
class_eval %Q( | |
def #{attr_name}=(attr_name) | |
@#{attr_name} = attr_name | |
unless @#{attr_name + "_history"} | |
@#{attr_name + "_history"} = [] | |
@#{attr_name + "_history"} << nil | |
end | |
@#{attr_name + "_history"} << attr_name | |
end | |
) | |
end | |
end |
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
class Numeric | |
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.0} | |
def method_missing(method_id) | |
singular_currency = method_id.to_s.gsub( /s$/, '') | |
if @@currencies.has_key?(singular_currency) | |
self * @@currencies[singular_currency] | |
else | |
super | |
end | |
end | |
def in(currency) | |
singular_currency = currency.to_s.gsub( /s$/, '') | |
if @@currencies.has_key?(singular_currency) | |
self / @@currencies[singular_currency] | |
else | |
nil | |
end | |
end | |
end | |
class String | |
def palindrome? | |
letters = self.gsub(/\W/, '').downcase | |
letters == letters.reverse | |
end | |
end | |
module Enumerable | |
def palindrome? | |
self.to_a == self.to_a.reverse | |
end | |
end | |
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
class CartesianProduct | |
include Enumerable | |
attr_accessor :first_sequence, :second_sequence | |
def initialize(first_sequence, second_sequence) | |
@first_sequence = first_sequence | |
@second_sequence = second_sequence | |
end | |
def each | |
self.first_sequence.each do |first_squence_element| | |
self.second_sequence.each do |second_sequence_element| | |
yield [first_squence_element, second_sequence_element] | |
end | |
end | |
end | |
end | |
c = CartesianProduct.new([:a,:b], [4,5]) | |
c.each { |elt| puts elt.inspect } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment