Guest User

Untitled

a guest
Nov 16th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 4.98 KB | None | 0 0
  1. def palindrome?(str)
  2.   lowercase_word_characters = str.gsub(/\W/, "").downcase
  3.   if lowercase_word_characters == lowercase_word_characters.reverse
  4.     return true
  5.   else
  6.     return false
  7.   end
  8. end
  9.  
  10. def count_words(str)
  11.   count = Hash.new(0)
  12.   words = str.split(/\W+/)
  13.   words.each do |word|
  14.     word.downcase!
  15.     count[word] += 1
  16.   end
  17. end
  18. class WrongNumberOfPlayersError < StandardError ; end
  19. class NoSuchStrategyError < StandardError ; end
  20.  
  21. def rps_game_winner(game)
  22.   raise WrongNumberOfPlayersError unless game.length == 2
  23.   strategies = {"r", "p", "s"}
  24.   player_1 = game[0]
  25.   player_2 = game[1]
  26.   strategy_1 = player_1[1].downcase
  27.   strategy_2 = player_2[1].downcase
  28.   raise NoSuchStrategyError unless
  29.     strategies.include?(strategy_1) && strategies.include?(strategy_2)
  30.   if strategy_1 == "r"
  31.     if strategy_2 == "s"
  32.       return player_1
  33.     end
  34.     if strategy_2 == "p"
  35.       return player_2
  36.     else
  37.       return player_1
  38.     end
  39.   end
  40.   if strategy_1 == "p"
  41.     if strategy_2 == "r"
  42.       return player_1
  43.     end
  44.     if strategy_2 == "s"
  45.       return player_2
  46.     else
  47.       return player_1
  48.     end
  49.   end
  50.   if strategy_1 == "s"
  51.     if strategy_2 == "r"
  52.       return player_2
  53.       end
  54.     if strategy_2 == "p"
  55.       return player_1
  56.     else
  57.       return player_1
  58.     end
  59.   end
  60. end
  61.  
  62. def rps_result(m1, m2)
  63.   # YOUR CODE HERE
  64. end
  65.  
  66. def rps_tournament_winner(tournament)
  67.   if tournament[0][0].class == String
  68.     return rps_game_winner(tournament)
  69.   else
  70.     winner_1 = rps_tournament_winner(tournament[0])
  71.     winner_2 = rps_tournament_winner(tournament[1])
  72.     return rps_game_winner(winner_1, winner_2)
  73.   end
  74. end
  75. def combine_anagrams(words)
  76.   anagram_groups = Hash.new
  77.   words.each do |word|
  78.     key = word.downcase.chars.sort.join
  79.     if anagram_groups.key?(key)
  80.       anagram_groups[key] += [word]
  81.     else
  82.       anagram_groups[key] = [word]
  83.     end
  84.   end
  85.   return anagram_groups.values
  86. end
  87. class Dessert
  88.   def initialize(name, calories)
  89.     @name = name
  90.     @calories = calories
  91.   end
  92.  
  93.   def calories
  94.     @calories
  95.   end
  96.  
  97.   def calories=(new_calories)
  98.     @calories = new_calories
  99.   end
  100.  
  101.   def name
  102.     @name
  103.   end
  104.  
  105.   def name=(new_name)
  106.     @name = new_name
  107.   end
  108.  
  109.   def healthy?
  110.     if self.calories < 200 #self needed? -> maybe thought of as nil (unassinged variable)
  111.       true
  112.     else
  113.       false
  114.     end
  115.   end
  116.  
  117.   def delicious?
  118.     true
  119.   end
  120. end
  121.  
  122. class JellyBean < Dessert
  123.   def initialize(name, calories, flavor)
  124.     @flavor = flavor
  125.     super(name, calories)
  126.   end
  127.  
  128.   def delicious?
  129.   end
  130. end
  131. class Class
  132.   def attr_accessor_with_history(attr_name)
  133.     attr_name = attr_name.to_s
  134.     attr_reader attr_name
  135.     class_eval %Q{
  136.       def #{attr_name}= (new_value)
  137.         if @#{attr_name}_history.nil?
  138.           @#{attr_name}_history = Array.new
  139.           @#{attr_name}_history += [@#{attr_name}]
  140.         end
  141.         #tried to call method on instance variable...
  142.         @#{attr_name} = new_value
  143.         @#{attr_name}_history += [@#{attr_name}]
  144.       end
  145.       def #{attr_name}_history
  146.         if @#{attr_name}_history.nil?
  147.           @#{attr_name}_history = Array.new
  148.           @#{attr_name}_history += [@#{attr_name}]
  149.         end
  150.         @#{attr_name}_history
  151.       end
  152.     }
  153.   end
  154. end
  155.  
  156. class Foo
  157.   attr_accessor_with_history :bar
  158.   attr_accessor_with_history :hum
  159. end
  160.  
  161. f = Foo.new
  162. f.bar = 1
  163. f.bar = 2
  164. f.bar_history # should be [nil, 1, 2]
  165. class Numeric
  166.   @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
  167.  
  168.   def in(target_currency)
  169.     singular_currency = target_currency.to_s.gsub(/s$/, '')
  170.     if @@currencies.has_key?(singular_currency)
  171.       self / @@currencies[singular_currency]
  172.     else
  173.       raise ArgumentError
  174.     end
  175.   end
  176.  
  177.   def method_missing(method_id)
  178.     singular_currency = method_id.to_s.gsub(/s$/, '')
  179.     if @@currencies.has_key?(singular_currency)
  180.       self * @@currencies[singular_currency]
  181.     else
  182.       super
  183.     end
  184.   end
  185. end
  186.  
  187. class String
  188.   def palindrome?
  189.     puts "string method"
  190.     lowercase_word_characters = self.gsub(/\W/, "").downcase
  191.     if lowercase_word_characters == lowercase_word_characters.reverse
  192.       true
  193.     else
  194.       false
  195.     end
  196.   end
  197. end
  198.  
  199. module Enumerable
  200.   def palindrome?
  201.     if self.to_a == self.to_a.reverse
  202.       true
  203.     else
  204.       false
  205.     end
  206.   end
  207. end
  208. class CartesianProduct
  209.  
  210.   def initialize collection_1, collection_2
  211.     @cartesian_product = Array.new
  212.     collection_1.each do |i|
  213.       collection_2.each do |j|
  214.         @cartesian_product += [[i, j]]
  215.       end
  216.     end
  217.   end
  218.  
  219.   def each(&block)
  220.     @cartesian_product.each(&block)
  221.   end
  222. end
  223.  
  224. #Examples of use
  225. c = CartesianProduct.new([:a,:b], [4,5])
  226. c.each { |elt| puts elt.inspect }
  227. # [:a, 4]
  228. # [:a, 5]
  229. # [:b, 4]
  230. # [:b, 5]
  231.  
  232. c = CartesianProduct.new([:a,:b], [])
  233. c.each { |elt| puts elt.inspect }
  234. # (nothing printed since Cartesian product
  235. # of anything with an empty collection is empty)
Add Comment
Please, Sign In to add comment