Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sumitk/6084424 to your computer and use it in GitHub Desktop.
Save sumitk/6084424 to your computer and use it in GitHub Desktop.

Revisions

  1. @tomtung tomtung renamed this gist Mar 9, 2012. 1 changed file with 68 additions and 1 deletion.
    69 changes: 68 additions & 1 deletion saas-class-homework1.rb → saas-class-homework-ruby-basics.rb
    Original 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]}]
    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
  2. @tomtung tomtung revised this gist Mar 9, 2012. 1 changed file with 59 additions and 59 deletions.
    118 changes: 59 additions & 59 deletions saas-class-homework1.rb
    Original 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
    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]}]
    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)
    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
    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
    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
    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 initialize(name, calories)
    @name = name
    @calories = calories
    end
    attr_accessor :name
    attr_accessor :calories

    def healthy?
    @calories < 200
    end
    def healthy?
    @calories < 200
    end

    def delicious?
    true
    end
    def delicious?
    true
    end
    end

    class JellyBean < Dessert
    attr_accessor :flavor
    def initialize(name, calories, flavor)
    super(name, calories)
    @flavor = flavor
    end
    attr_accessor :flavor
    def initialize(name, calories, flavor)
    super(name, calories)
    @flavor = flavor
    end

    def delicious?
    @flavor != "black licorice"
    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
    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
    @#{attr_name} = value
    @#{attr_name}_history << value
    end
    }
    end
    end
  3. @tomtung tomtung revised this gist Mar 9, 2012. 1 changed file with 65 additions and 65 deletions.
    130 changes: 65 additions & 65 deletions saas-class-homework1.rb
    Original file line number Diff line number Diff line change
    @@ -1,97 +1,97 @@
    # Part 1
    # Homework 1 - Part 1
    def palindrome?(string)
    string = string.downcase.gsub(/\W/, '')
    string == string.reverse
    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]}]
    Hash[string.downcase.scan(/\w+/).group_by{|s| s}.map{|kv| [kv[0], kv[1].size]}]
    end

    # Part 2
    # 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)
    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
    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
    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
    # Homework 1 - Part 3
    def combine_anagrams(words)
    words.group_by{|w| w.downcase.chars.sort.to_s}.values
    words.group_by{|w| w.downcase.chars.sort.to_s}.values
    end

    # Part 4
    # Homework 1 - Part 4
    class Dessert
    def initialize(name, calories)
    @name = name
    @calories = calories
    end
    attr_accessor :name
    attr_accessor :calories
    def initialize(name, calories)
    @name = name
    @calories = calories
    end
    attr_accessor :name
    attr_accessor :calories

    def healthy?
    @calories < 200
    end
    def healthy?
    @calories < 200
    end

    def delicious?
    true
    end
    def delicious?
    true
    end
    end

    class JellyBean < Dessert
    attr_accessor :flavor
    def initialize(name, calories, flavor)
    super(name, calories)
    @flavor = flavor
    end
    attr_accessor :flavor
    def initialize(name, calories, flavor)
    super(name, calories)
    @flavor = flavor
    end

    def delicious?
    @flavor != "black licorice"
    end
    def delicious?
    @flavor != "black licorice"
    end
    end

    # Part 5
    # 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
    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
    @#{attr_name} = value
    @#{attr_name}_history << value
    end
    }
    end
    end
  4. @tomtung tomtung revised this gist Mar 5, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion saas-class-homework1.rb
    Original 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

    require 'set'
    def lose?(mine, his)
    mine.upcase!; his.upcase!
    strategies = ['R', 'P', 'S']
  5. @tomtung tomtung revised this gist Mar 4, 2012. No changes.
  6. @tomtung tomtung revised this gist Mar 4, 2012. No changes.
  7. @tomtung tomtung created this gist Mar 4, 2012.
    98 changes: 98 additions & 0 deletions saas-class-homework1.rb
    Original 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