From: Daniel Sheppard Date: 2005-11-22T07:27:07+09:00 Subject: Re: [QUIZ SOLUTION] Euchre Hands (#55) Because I'm an idiot and didn't read the quiz spec properly, or test my code properly - I left aces low, and expected all the number cards in there, and made a couple of other typos. I also made it faster by calculating things only once in the initialize instead of constantly in the sort_value method. Fixed now: class Card attr_reader :suite, :value def initialize(string) @value = string[0..-2].upcase @suite = string[-1..-1].downcase end def to_i %w{9 T J Q K A}.index(@value) end def to_s @value + @suite end end class EuchreHand def initialize(trump, cards) @trump_string = trump @trump = trump[0..0].downcase @cards = cards same, opposite = [['h','d'],['c','s']].partition {|x| x.include?(@trump)} same[0].delete(@trump) @same = same[0][0] @suite_order = [@trump, opposite[0][0], same, opposite[0][1]] end def to_s ([@trump_string] +sorted_cards).join("\n") end def sorted_cards @cards.sort_by {|c| sort_value(c)} end private #there are 6 sections of the sort: jack1, jack2, trump, opp1, same, opp2 def sort_value(card) return [0] if card.value == 'J' && card.suite == @trump return [1] if card.value == 'J' && card.suite == @same return [2+@suite_order.index(card.suite),-card.to_i] end end cards = ARGF.readlines puts EuchreHand.new(cards.shift, cards.map {|s| Card.new(s)}) ##################################################################################### This email has been scanned by MailMarshal, an email content filter. #####################################################################################