--TB36FDmn/VVEgNH/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Here's my minimalist solution: 20 lines.  I believe it always gets
both rank and color-alternation correct.  If you only want to get
alternation correct *most* of the time, you can cut out another five
lines.  :-)

SUITS =3D %w{d c h s}.map {|s| s[0]}
CARDS =3D %w{A K Q J T 9}.map {|c| c[0]}

trump,hand =3D STDIN.readline, STDIN.readlines

puts trump
trump =3D SUITS.index(trump.downcase[0])

# If the suit after the trump suit is missing, we swap it with the
# other suit of the same color.  This ensures that we always have a
# correct color alternation when possible.
unless hand.find {|card| card[1] =3D=3D SUITS[(trump+1)%4]}
  tmp =3D SUITS[(trump+1)%4]
  SUITS[(trump+1)%4] =3D SUITS[(trump+3)%4]
  SUITS[(trump+3)%4] =3D tmp
end

hand.map { |card|=20
  suit =3D (SUITS.index(card[1]) - trump)%4
  num =3D CARDS.index(card[0])
  if num=3D=3D3 && suit=3D=3D2
    suit,num =3D 0,-1             # Left bower
  elsif num=3D=3D3 && suit=3D=3D0
    num =3D -2                    # Right bower
  end
  [suit,num,card.chomp]
}.sort.each{|c| puts "#{c[2]}\n" }


--TB36FDmn/VVEgNH/
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDgJmYnhUz11p9MSARAt1cAKC8/V15WvvXCnxPxtmkvOPHLS9KRwCbBu4G
9+kkX5s6DVtZP4uGzXHKX5g=
=EaVm
-----END PGP SIGNATURE-----

--TB36FDmn/VVEgNH/--

Here's my minimalist solution: 20 lines.  I believe it always gets
both rank and color-alternation correct.  If you only want to get
alternation correct *most* of the time, you can cut out another five
lines.  :-)

SUITS =3D %w{d c h s}.map {|s| s[0]}
CARDS =3D %w{A K Q J T 9}.map {|c| c[0]}

trump,hand =3D STDIN.readline, STDIN.readlines

puts trump
trump =3D SUITS.index(trump.downcase[0])

# If the suit after the trump suit is missing, we swap it with the
# other suit of the same color.  This ensures that we always have a
# correct color alternation when possible.
unless hand.find {|card| card[1] =3D=3D SUITS[(trump+1)%4]}
  tmp =3D SUITS[(trump+1)%4]
  SUITS[(trump+1)%4] =3D SUITS[(trump+3)%4]
  SUITS[(trump+3)%4] =3D tmp
end

hand.map { |card|=20
  suit =3D (SUITS.index(card[1]) - trump)%4
  num =3D CARDS.index(card[0])
  if num=3D=3D3 && suit=3D=3D2
    suit,num =3D 0,-1             # Left bower
  elsif num=3D=3D3 && suit=3D=3D0
    num =3D -2                    # Right bower
  end
  [suit,num,card.chomp]
}.sort.each{|c| puts "#{c[2]}\n" }

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDgJmYnhUz11p9MSARAt1cAKC8/V15WvvXCnxPxtmkvOPHLS9KRwCbBu4G
9+kkX5s6DVtZP4uGzXHKX5g=
=EaVm
-----END PGP SIGNATURE-----