简体   繁体   English

检查以特定字符串开头的列表

[英]Check a list that starts with a specific string

I'm down to creating this method called is ordered which needs the following function. 我打算创建一个称为ordered的方法,该方法需要以下功能。

When the list or sorted deck is sorted, it starts from the lowest to highest with 2C (2 of Clovers). 对列表或排序的卡片组进行排序时,它从最低到最高为2C(2个三叶草)。

import random

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

er.num) return t1 == t2 er.num)返回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) . 您可以将self.deck的列表与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 我使用了functools.total_orderingdoc ),因此只需__eq____lt__即可实现

  • make num_rank property through @property decorator 使num_rank通过财产@property装饰

  • the sorting is now working by (suit, num_rank) - that's how __eq__ and __lt__ are defined. 排序现在通过(suit, num_rank) -这就是__eq____lt__的定义方式。 Maybe parametrizing isOrdered() should be taken into consideration - isOrdered(by suit or by num etc...) 也许应该考虑参数isOrdered()参数isOrdered() 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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