By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

need help with a sorting problem

P: 8
i need to sort a list of card like the rank
sample: ACE of h, ACE of d, ACE of c, ACE of s, 2, 2, 2, 2
this is my code so can anyone help me???


Expand|Select|Wrap|Line Numbers
  1. class Card:
  2.  
  3.     suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
  4.     rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
  5.                 "8", "9", "10", "Jack", "Queen", "King"]
  6.  
  7.     def __init__ (self, suit=0, rank=1):
  8.         if suit >= 0 and suit <= 3:
  9.             self.suit = suit
  10.         else:
  11.             self.suit = 0
  12.         if rank >= 1 and rank <= 13:
  13.             self.rank = rank
  14.         else:
  15.             self.rank = 1
  16.  
  17.     def __str__(self):
  18.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  19.  
  20.  
  21. class Deck:
  22.  
  23.     def __init__(self):
  24.         self.cards.sort
  25.         self.cards = [] 
  26.         for suit in range(4):
  27.             for rank in range(1,14):
  28.                 self.cards.append(Card(suit, rank))
  29.  
  30.  
  31.     def __str__(self):
  32.         s = ""
  33.         for i in range(len(self.cards)):        
  34.             s = s + (" " * i) + str(self.cards[i]) + "\n"
  35.         return s
  36.  
  37.     def __str__(self):
  38.         return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
  39.  
  40. def main():
  41.     card_deck = Deck()
  42.     print "Original deck:"
  43.     print card_deck
  44.     print
  45.  
  46. main()
May 25 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
i need to sort a list of card like the rank
sample: ACE of h, ACE of d, ACE of c, ACE of s, 2, 2, 2, 2
this is my code so can anyone help me???


Expand|Select|Wrap|Line Numbers
  1. class Card:
  2.  
  3.     suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
  4.     rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
  5.                 "8", "9", "10", "Jack", "Queen", "King"]
  6.  
  7.     def __init__ (self, suit=0, rank=1):
  8.         if suit >= 0 and suit <= 3:
  9.             self.suit = suit
  10.         else:
  11.             self.suit = 0
  12.         if rank >= 1 and rank <= 13:
  13.             self.rank = rank
  14.         else:
  15.             self.rank = 1
  16.  
  17.     def __str__(self):
  18.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  19.  
  20.  
  21. class Deck:
  22.  
  23.     def __init__(self):
  24.         self.cards.sort
  25.         self.cards = [] 
  26.         for suit in range(4):
  27.             for rank in range(1,14):
  28.                 self.cards.append(Card(suit, rank))
  29.  
  30.  
  31.     def __str__(self):
  32.         s = ""
  33.         for i in range(len(self.cards)):        
  34.             s = s + (" " * i) + str(self.cards[i]) + "\n"
  35.         return s
  36.  
  37.     def __str__(self):
  38.         return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
  39.  
  40. def main():
  41.     card_deck = Deck()
  42.     print "Original deck:"
  43.     print card_deck
  44.     print
  45.  
  46. main()
Card() method:
Expand|Select|Wrap|Line Numbers
  1. ....def __cmp__(self, other):
  2.         i = cmp(self.rank, other.rank)
  3.         if i == 0:
  4.             return cmp(self.suit, other.suit)
  5.         return i
Deck() method to allow index access to individual cards:
Expand|Select|Wrap|Line Numbers
  1. ....def __getitem__(self, i):
  2.         return self.cards[i]
>>> deck1[50]
Queen of Spades
>>> deck1[24]
Queen of Diamonds
>>> deck1[50]>deck1[24]
True
>>> deck1[24]>deck1[50]
False
>>> deck1.cards.sort()
>>> for card in deck1.cards:
... print card
...
Ace of Hearts
Ace of Diamonds
Ace of Clubs
Ace of Spades
2 of Hearts
2 of Diamonds
2 of Clubs
2 of Spades
3 of Hearts
3 of Diamonds
3 of Clubs
3 of Spades
4 of Hearts
4 of Diamonds
4 of Clubs
4 of Spades
5 of Hearts
5 of Diamonds
5 of Clubs
5 of Spades
6 of Hearts
6 of Diamonds
6 of Clubs
6 of Spades
7 of Hearts
7 of Diamonds
7 of Clubs
7 of Spades
8 of Hearts
8 of Diamonds
8 of Clubs
8 of Spades
9 of Hearts
9 of Diamonds
9 of Clubs
9 of Spades
10 of Hearts
10 of Diamonds
10 of Clubs
10 of Spades
Jack of Hearts
Jack of Diamonds
Jack of Clubs
Jack of Spades
Queen of Hearts
Queen of Diamonds
Queen of Clubs
Queen of Spades
King of Hearts
King of Diamonds
King of Clubs
King of Spades
>>>
May 25 '07 #2

P: 8
thanks again lol you are a life saver
May 25 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.