Add, remove, shuffle and sort

Source: Internet
Author: User

To Deal cards, we wowould like a method that removes a card from the deck and returns it. the list method pop provides a convenient way to do that. since POP removes the last card in the list, we are in effect dealing from the bottom of the deck. to add a card, we can use the list method append. as another example, we can write a deck method named shuffle using the function shuffle from the random module.

    def pop_card(self):        return self.cards.pop()    def add_card(self,card):        return self.cards.append(card)    def shuffle(self):        random.shuffle(self.cards)    def sort(self):        for i in range(len(self.cards)):            for j in range(i+1,len(self.cards)):                if(self.cards[i].cmp(self.cards[j])>0):                    self.cards[i],self.cards[j] = self.cards[j],self.cards[i]

A method like this that uses another function without doing much real work is sometimes called a veneer, the metaphor comes from woodworking, where it is common to glue a thin layer of good quality wood to the surface of a cheaper piece of wood.

From Thinking in Python

Add, remove, shuffle and sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.