Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 96e5ba05cae9a1f5c103b0b7c0d625b6af91099d @mnipper committed Jan 20, 2013
Showing with 159 additions and 0 deletions.
  1. +18 −0 part1.rb
  2. +28 −0 part2.rb
  3. +14 −0 part3.rb
  4. +28 −0 part4.rb
  5. +18 −0 part5.rb
  6. +36 −0 part6.rb
  7. +17 −0 part7.rb
@@ -0,0 +1,18 @@
+#For those just starting out, one suggested way to work on your code is to have a command window open and a text editor with this
+#file loaded. Make changes to this file and then run 'ruby part1.rb' at the command line, this will run your program. Once you're
+#satisfied with your work, save your file and upload it to the checker.
+
+
+def palindrome?(str)
+ phrase = str.scan(/\w+/).join.downcase
+ phrase == phrase.reverse
+end
+
+def count_words(str)
+ h = Hash.new
+ words = str.downcase.scan(/\w+/)
+ words.each do |word|
+ h[word] = h.has_key?(word) ? h[word]+1 : 1
+ end
+ h
+end
@@ -0,0 +1,28 @@
+class WrongNumberOfPlayersError < StandardError ; end
+class NoSuchStrategyError < StandardError ; end
+
+def rps_result(m1, m2)
+ wins = [ ['R', 'S'], ['S', 'P'], ['P', 'R'] ]
+ wins.each do |w|
+ return m2 if m1[1].upcase == w[1] and m2[1].upcase == w[0]
+ end
+ m1
+end
+
+def rps_game_winner(game)
+ raise WrongNumberOfPlayersError unless game.length == 2
+ game.each do |m|
+ raise NoSuchStrategyError unless m[1] =~ /^(R|P|S)$/i
+ end
+ rps_result(game[0], game[1])
+end
+
+def rps_tournament_winner(tournament)
+ if tournament.flatten(1).first.kind_of? String
+ rps_game_winner(tournament)
+ else
+ winner(Array.new([winner(tournament[0]), winner(tournament[1])]))
+ end
+end
+
+alias :winner :rps_tournament_winner
@@ -0,0 +1,14 @@
+class String
+ def sort_letters
+ self.downcase.split(%r{\s*}).sort.join
+ end
+end
+
+def combine_anagrams(words)
+ anagrams = []
+ words.each do |word|
+ anagram = words.select {|w| w.sort_letters == word.sort_letters }
+ anagrams << anagram
+ end
+ anagrams.uniq
+end
@@ -0,0 +1,28 @@
+class Dessert
+ attr_accessor :name, :calories
+
+ def initialize(name, calories)
+ @name = name
+ @calories = calories
+ end
+
+ def healthy?
+ @calories < 200 ? true : false
+ end
+
+ def delicious?
+ true
+ end
+end
+
+class JellyBean < Dessert
+ attr_accessor :flavor
+
+ def initialize(name, calories, flavor)
+ @flavor = flavor
+ end
+
+ def delicious?
+ @flavor == "black licorice" ? false : super
+ end
+end
@@ -0,0 +1,18 @@
+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 bar_history getter
+ class_eval %Q[
+ def #{attr_name}= val
+ @#{attr_name} = val
+ if defined? @#{attr_name}_history
+ @#{attr_name}_history.push(val)
+ else
+ @#{attr_name}_history = []
+ @#{attr_name}_history << nil << val
+ end
+ end
+ ]
+ end
+end
@@ -0,0 +1,36 @@
+class String
+ def singularize
+ self.gsub(/s$/, '')
+ end
+
+ def letters
+ self.gsub(/\W+/, '')
+ end
+
+ def palindrome?
+ self.letters.downcase == self.letters.downcase.reverse
+ end
+end
+
+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.singularize
+ if @@currencies.has_key?(singular_currency)
+ self * @@currencies[singular_currency]
+ else
+ super
+ end
+ end
+
+ def in(currency)
+ self / @@currencies[currency.to_s.singularize]
+ end
+end
+
+
+module Enumerable
+ def palindrome?
+ self.to_a == self.to_a.reverse
+ end
+end
@@ -0,0 +1,17 @@
+class CartesianProduct
+ include Enumerable
+ attr_accessor :a, :b
+
+ def initialize(a, b)
+ @a = a
+ @b = b
+ end
+
+ def each
+ @a.each do |a|
+ @b.each do |b|
+ yield [a,b]
+ end
+ end
+ end
+end

0 comments on commit 96e5ba0

Please sign in to comment.