简体   繁体   中英

Check a list that starts with a specific string

I'm down to creating this method called is ordered which needs the following function.

When the list or sorted deck is sorted, it starts from the lowest to highest with 2C (2 of Clovers).

import random

class Card(object):
    def __init__(self, num, suit):
        self.num = num
        self.suit = suit

er.num) return t1 == t2

    def num_rank(num):
        if num[0] == "A":
            return 14
        if num[0] == "J":
            return 11
        if num[0] == "Q":
            return 12
        if num[0] == "K":
            return 13
        return int(num)

class Deck(object):
    def __init__
        self.m for s in self.suit]


    def isOrdered(self):
        if self. str('2C'):
            return True

You can compare the the list in self.deck to sorted(self.deck) . If they are equal, the deck is ordered:

from functools import total_ordering

@total_ordering
class Card(object):
    def __init__(self, num, suit):
        self.num = num
        self.suit = suit

    def __str__(self):
        return '%s%s' % (self.num,
                         self.suit)

    def __repr__(self): return str(self)

    def __lt__(self, other):
        t1 = self.suit, self.num_rank
        t2 = other.suit, other.num_rank
        return t1 < t2

    def __eq__(self, other):
        t1 = self.suit, self.num_rank
        t2 = other.suit, other.num_rank
        return t1 == t2

    @property
    def num_rank(self):
        if self.num[0] == "A":
            return 14
        if self.num[0] == "J":
            return 11
        if self.num[0] == "Q":
            return 12
        if self.num[0] == "K":
            return 13
        return int(self.num)

class Deck(object):
    def __init__(self):
        self.num = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
        self.suit = ['C', 'D', 'H', 'S']
        self.deck = [Card(r, s) for r in self.num for s in self.suit]

    def isOrdered(self):
        print('My deck :', self.deck)
        print('My sorted deck :', sorted(self.deck))

        return self.deck == sorted(self.deck)

d = Deck()
print('Deck.isOrdered() ==', d.isOrdered())

Prints:

My deck : [2C, 2D, 2H, 2S, 3C, 3D, 3H, 3S, 4C, 4D, 4H, 4S, 5C, 5D, 5H, 5S, 6C, 6D, 6H, 6S, 7C, 7D, 7H, 7S, 8C, 8D, 8H, 8S, 9C, 9D, 9H, 9S, 10C, 10D, 10H, 10S, JC, JD, JH, JS, QC, QD, QH, QS, KC, KD, KH, KS, AC, AD, AH, AS]
My sorted deck : [2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD, AD, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH, AH, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS, AS]
Deck.isOrdered() == False

NOTE:

  • I used functools.total_ordering ( doc ), so only __eq__ and __lt__ is necessary to implement

  • make num_rank property through @property decorator

  • the sorting is now working by (suit, num_rank) - that's how __eq__ and __lt__ are defined. Maybe parametrizing isOrdered() should be taken into consideration - isOrdered(by suit or by num etc...)

So likely what you will need to do is to loop through the deck and see if any of the cards are out of order. In other words,

in_order = True 
for c in range(len(self.deck)-1): 
    if self.deck[c] > self.deck[c+1]:
        in_order = False
        break
return in_order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM