need help with a sorting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codie
    New Member
    • May 2007
    • 8

    #1

    need help with a sorting problem

    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???


    Code:
    class Card:
    
        suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
        rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
                    "8", "9", "10", "Jack", "Queen", "King"]
    
        def __init__ (self, suit=0, rank=1):
            if suit >= 0 and suit <= 3:
                self.suit = suit
            else:
                self.suit = 0
            if rank >= 1 and rank <= 13:
                self.rank = rank
            else:
                self.rank = 1
    
        def __str__(self):
            return self.rankList[self.rank] + " of " + self.suitList[self.suit]
            
    
    class Deck:
    
        def __init__(self):
            self.cards.sort
            self.cards = [] 
            for suit in range(4):
                for rank in range(1,14):
                    self.cards.append(Card(suit, rank))
                    
    
        def __str__(self):
            s = ""
            for i in range(len(self.cards)):        
                s = s + (" " * i) + str(self.cards[i]) + "\n"
            return s
                
        def __str__(self):
            return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
       
    def main():
        card_deck = Deck()
        print "Original deck:"
        print card_deck
        print
        
    main()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by codie
    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???


    Code:
    class Card:
    
        suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
        rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
                    "8", "9", "10", "Jack", "Queen", "King"]
    
        def __init__ (self, suit=0, rank=1):
            if suit >= 0 and suit <= 3:
                self.suit = suit
            else:
                self.suit = 0
            if rank >= 1 and rank <= 13:
                self.rank = rank
            else:
                self.rank = 1
    
        def __str__(self):
            return self.rankList[self.rank] + " of " + self.suitList[self.suit]
            
    
    class Deck:
    
        def __init__(self):
            self.cards.sort
            self.cards = [] 
            for suit in range(4):
                for rank in range(1,14):
                    self.cards.append(Card(suit, rank))
                    
    
        def __str__(self):
            s = ""
            for i in range(len(self.cards)):        
                s = s + (" " * i) + str(self.cards[i]) + "\n"
            return s
                
        def __str__(self):
            return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
       
    def main():
        card_deck = Deck()
        print "Original deck:"
        print card_deck
        print
        
    main()
    Card() method:[code=Python]....def __cmp__(self, other):
    i = cmp(self.rank, other.rank)
    if i == 0:
    return cmp(self.suit, other.suit)
    return i[/code]Deck() method to allow index access to individual cards:[code=Python]....def __getitem__(sel f, i):
    return self.cards[i][/code]>>> deck1[50]
    Queen of Spades
    >>> deck1[24]
    Queen of Diamonds
    >>> deck1[50]>deck1[24]
    True
    >>> deck1[24]>deck1[50]
    False
    >>> deck1.cards.sor t()
    >>> 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
    >>>

    Comment

    • codie
      New Member
      • May 2007
      • 8

      #3
      thanks again lol you are a life saver

      Comment

      Working...