Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def palindrome?(str)
- lowercase_word_characters = str.gsub(/\W/, "").downcase
- if lowercase_word_characters == lowercase_word_characters.reverse
- return true
- else
- return false
- end
- end
- def count_words(str)
- count = Hash.new(0)
- words = str.split(/\W+/)
- words.each do |word|
- word.downcase!
- count[word] += 1
- end
- end
- class WrongNumberOfPlayersError < StandardError ; end
- class NoSuchStrategyError < StandardError ; end
- def rps_game_winner(game)
- raise WrongNumberOfPlayersError unless game.length == 2
- strategies = {"r", "p", "s"}
- player_1 = game[0]
- player_2 = game[1]
- strategy_1 = player_1[1].downcase
- strategy_2 = player_2[1].downcase
- raise NoSuchStrategyError unless
- strategies.include?(strategy_1) && strategies.include?(strategy_2)
- if strategy_1 == "r"
- if strategy_2 == "s"
- return player_1
- end
- if strategy_2 == "p"
- return player_2
- else
- return player_1
- end
- end
- if strategy_1 == "p"
- if strategy_2 == "r"
- return player_1
- end
- if strategy_2 == "s"
- return player_2
- else
- return player_1
- end
- end
- if strategy_1 == "s"
- if strategy_2 == "r"
- return player_2
- end
- if strategy_2 == "p"
- return player_1
- else
- return player_1
- end
- end
- end
- def rps_result(m1, m2)
- # YOUR CODE HERE
- end
- def rps_tournament_winner(tournament)
- if tournament[0][0].class == String
- return rps_game_winner(tournament)
- else
- winner_1 = rps_tournament_winner(tournament[0])
- winner_2 = rps_tournament_winner(tournament[1])
- return rps_game_winner(winner_1, winner_2)
- end
- end
- def combine_anagrams(words)
- anagram_groups = Hash.new
- words.each do |word|
- key = word.downcase.chars.sort.join
- if anagram_groups.key?(key)
- anagram_groups[key] += [word]
- else
- anagram_groups[key] = [word]
- end
- end
- return anagram_groups.values
- end
- class Dessert
- def initialize(name, calories)
- @name = name
- @calories = calories
- end
- def calories
- @calories
- end
- def calories=(new_calories)
- @calories = new_calories
- end
- def name
- @name
- end
- def name=(new_name)
- @name = new_name
- end
- def healthy?
- if self.calories < 200 #self needed? -> maybe thought of as nil (unassinged variable)
- true
- else
- false
- end
- end
- def delicious?
- true
- end
- end
- class JellyBean < Dessert
- def initialize(name, calories, flavor)
- @flavor = flavor
- super(name, calories)
- end
- def delicious?
- end
- end
- class Class
- def attr_accessor_with_history(attr_name)
- attr_name = attr_name.to_s
- attr_reader attr_name
- class_eval %Q{
- def #{attr_name}= (new_value)
- if @#{attr_name}_history.nil?
- @#{attr_name}_history = Array.new
- @#{attr_name}_history += [@#{attr_name}]
- end
- #tried to call method on instance variable...
- @#{attr_name} = new_value
- @#{attr_name}_history += [@#{attr_name}]
- end
- def #{attr_name}_history
- if @#{attr_name}_history.nil?
- @#{attr_name}_history = Array.new
- @#{attr_name}_history += [@#{attr_name}]
- end
- @#{attr_name}_history
- end
- }
- end
- end
- class Foo
- attr_accessor_with_history :bar
- attr_accessor_with_history :hum
- end
- f = Foo.new
- f.bar = 1
- f.bar = 2
- f.bar_history # should be [nil, 1, 2]
- class Numeric
- @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
- def in(target_currency)
- singular_currency = target_currency.to_s.gsub(/s$/, '')
- if @@currencies.has_key?(singular_currency)
- self / @@currencies[singular_currency]
- else
- raise ArgumentError
- end
- end
- 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
- end
- class String
- def palindrome?
- puts "string method"
- lowercase_word_characters = self.gsub(/\W/, "").downcase
- if lowercase_word_characters == lowercase_word_characters.reverse
- true
- else
- false
- end
- end
- end
- module Enumerable
- def palindrome?
- if self.to_a == self.to_a.reverse
- true
- else
- false
- end
- end
- end
- class CartesianProduct
- def initialize collection_1, collection_2
- @cartesian_product = Array.new
- collection_1.each do |i|
- collection_2.each do |j|
- @cartesian_product += [[i, j]]
- end
- end
- end
- def each(&block)
- @cartesian_product.each(&block)
- end
- end
- #Examples of use
- c = CartesianProduct.new([:a,:b], [4,5])
- c.each { |elt| puts elt.inspect }
- # [:a, 4]
- # [:a, 5]
- # [:b, 4]
- # [:b, 5]
- c = CartesianProduct.new([:a,:b], [])
- c.each { |elt| puts elt.inspect }
- # (nothing printed since Cartesian product
- # of anything with an empty collection is empty)
Add Comment
Please, Sign In to add comment