Forked from tomtung/saas-class-homework-ruby-basics.rb
Created
July 25, 2013 22:40
-
-
Save sumitk/6084424 to your computer and use it in GitHub Desktop.
Revisions
-
tomtung renamed this gist
Mar 9, 2012 . 1 changed file with 68 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,10 @@ def palindrome?(string) end def count_words(string) Hash[ string.downcase.scan(/\w+/). group_by{|s| s}. map{|kv| [kv[0], kv[1].size]}] end # Homework 1 - Part 2 @@ -95,3 +98,67 @@ def #{attr_name}=(value) } end end # Homework 2 - 1 - a class Numeric @@currencies = {'dollar' => 1.0, 'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019} def method_missing(method_id) singular_currency = to_singular(method_id) if @@currencies.has_key?(singular_currency) self * @@currencies[singular_currency] else super end end def in(currency) singular_currency = to_singular(currency) if @@currencies.has_key?(singular_currency) self / @@currencies[singular_currency] else super end end private def to_singular(word) word.to_s.gsub( /s$/, '') end end # Homework 2 - 1 - b class String def palindrome? # Calls palindrome?(s) defined in Homework 1 Object.send(:palindrome?, self) end end # Homework 2 - 1 - c module Enumerable def palindrome? self.collect{|x| x} == self.collect{|x| x}.reverse end end # Homework 2 - 2 class CartesianProduct include Enumerable attr_reader :l_enum, :r_enum def initialize(l_enum, r_enum) @l_enum = l_enum @r_enum = r_enum end def each self.l_enum.each { |l| self.r_enum.each { |r| yield [l, r] } } end end -
tomtung revised this gist
Mar 9, 2012 . 1 changed file with 59 additions and 59 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,97 +1,97 @@ # Homework 1 - Part 1 def palindrome?(string) string = string.downcase.gsub(/\W/, '') string == string.reverse end def count_words(string) Hash[string.downcase.scan(/\w+/).group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}] end # Homework 1 - Part 2 class WrongNumberOfPlayersError < StandardError ; end class NoSuchStrategyError < StandardError ; end def lose?(mine, his) mine.upcase!; his.upcase! strategies = ['R', 'P', 'S'] if(!strategies.include?(mine) or !strategies.include?(his)) raise NoSuchStrategyError end ["RP", "PS", "SR"].include?(mine + his) end def rps_game_winner(game) raise WrongNumberOfPlayersError unless game.length == 2 if(lose?(game[0][1], game[1][1])) game[1] else game[0] end end def rps_tournament_winner(game) if(game[0][0].kind_of?(String)) rps_game_winner(game) else rps_game_winner [rps_tournament_winner(game[0]), rps_tournament_winner(game[1])] end end # Homework 1 - Part 3 def combine_anagrams(words) words.group_by{|w| w.downcase.chars.sort.to_s}.values end # Homework 1 - Part 4 class Dessert def initialize(name, calories) @name = name @calories = calories end attr_accessor :name attr_accessor :calories def healthy? @calories < 200 end def delicious? true end end class JellyBean < Dessert attr_accessor :flavor def initialize(name, calories, flavor) super(name, calories) @flavor = flavor end def delicious? @flavor != "black licorice" end end # Homework 1 - Part 5 class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_reader attr_name+"_history" class_eval %Q{ def #{attr_name}=(value) if(!defined?(@#{attr_name}_history)) @#{attr_name}_history = [@#{attr_name}] end @#{attr_name} = value @#{attr_name}_history << value end } end end -
tomtung revised this gist
Mar 9, 2012 . 1 changed file with 65 additions and 65 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,97 +1,97 @@ # Homework 1 - Part 1 def palindrome?(string) string = string.downcase.gsub(/\W/, '') string == string.reverse end def count_words(string) Hash[string.downcase.scan(/\w+/).group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}] end # Homework 1 - Part 2 class WrongNumberOfPlayersError < StandardError ; end class NoSuchStrategyError < StandardError ; end def lose?(mine, his) mine.upcase!; his.upcase! strategies = ['R', 'P', 'S'] if(!strategies.include?(mine) or !strategies.include?(his)) raise NoSuchStrategyError end ["RP", "PS", "SR"].include?(mine + his) end def rps_game_winner(game) raise WrongNumberOfPlayersError unless game.length == 2 if(lose?(game[0][1], game[1][1])) game[1] else game[0] end end def rps_tournament_winner(game) if(game[0][0].kind_of?(String)) rps_game_winner(game) else rps_game_winner [rps_tournament_winner(game[0]), rps_tournament_winner(game[1])] end end # Homework 1 - Part 3 def combine_anagrams(words) words.group_by{|w| w.downcase.chars.sort.to_s}.values end # Homework 1 - Part 4 class Dessert def initialize(name, calories) @name = name @calories = calories end attr_accessor :name attr_accessor :calories def healthy? @calories < 200 end def delicious? true end end class JellyBean < Dessert attr_accessor :flavor def initialize(name, calories, flavor) super(name, calories) @flavor = flavor end def delicious? @flavor != "black licorice" end end # Homework 1 - Part 5 class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_reader attr_name+"_history" class_eval %Q{ def #{attr_name}=(value) if(!defined?(@#{attr_name}_history)) @#{attr_name}_history = [@#{attr_name}] end @#{attr_name} = value @#{attr_name}_history << value end } end end -
tomtung revised this gist
Mar 5, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,6 @@ def count_words(string) class WrongNumberOfPlayersError < StandardError ; end class NoSuchStrategyError < StandardError ; end def lose?(mine, his) mine.upcase!; his.upcase! strategies = ['R', 'P', 'S'] -
tomtung revised this gist
Mar 4, 2012 . No changes.There are no files selected for viewing
-
tomtung revised this gist
Mar 4, 2012 . No changes.There are no files selected for viewing
-
tomtung created this gist
Mar 4, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,98 @@ # Part 1 def palindrome?(string) string = string.downcase.gsub(/\W/, '') string == string.reverse end def count_words(string) Hash[string.downcase.gsub(/[^\w\s]/,'').split(' ').group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}] end # Part 2 class WrongNumberOfPlayersError < StandardError ; end class NoSuchStrategyError < StandardError ; end require 'set' def lose?(mine, his) mine.upcase!; his.upcase! strategies = ['R', 'P', 'S'] if(!strategies.include?(mine) or !strategies.include?(his)) raise NoSuchStrategyError end ["RP", "PS", "SR"].include?(mine + his) end def rps_game_winner(game) raise WrongNumberOfPlayersError unless game.length == 2 if(lose?(game[0][1], game[1][1])) game[1] else game[0] end end def rps_tournament_winner(game) if(game[0][0].kind_of?(String)) rps_game_winner(game) else rps_game_winner [rps_tournament_winner(game[0]), rps_tournament_winner(game[1])] end end # Part 3 def combine_anagrams(words) words.group_by{|w| w.downcase.chars.sort.to_s}.values end # Part 4 class Dessert def initialize(name, calories) @name = name @calories = calories end attr_accessor :name attr_accessor :calories def healthy? @calories < 200 end def delicious? true end end class JellyBean < Dessert attr_accessor :flavor def initialize(name, calories, flavor) super(name, calories) @flavor = flavor end def delicious? @flavor != "black licorice" end end # Part 5 class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_reader attr_name+"_history" class_eval %Q{ def #{attr_name}=(value) if(!defined?(@#{attr_name}_history)) @#{attr_name}_history = [@#{attr_name}] end @#{attr_name} = value @#{attr_name}_history << value end } end end