【每日一题20220317】扑克荷官

:woman_mage:一副扑克牌总共包含52张牌,花色分别用H表示红桃:heart:、C表示梅花:clubs:、D表示:diamonds:、S表示:spades:,牌面点数A~K则分别从数字1到13表示。

我们的任务就是,编写一个函数,随机生成不重复的一整副牌列表。每张牌的格式为"花色 点数",例如红桃Q表示为“H 12”,黑桃A表示为“S 1”。每次生成的牌序必须是不同的。

题目难度:中等
题目来源:CodeWars:Deal a Shuffled Deck of Cards

def solution() -> list:
    # your code here

deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
def solution() -> list:
    # 0317 扑克荷官
    res = []
    icons = ['H', 'C', 'D', 'S']
    random.shuffle(icons)
    for i in icons:
        nums = [i for i in range(1, 14)]
        random.shuffle(nums)
        for j in nums:
            tmp = (i, j)
            res.append(tmp)
    print(res)
    return res

deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
1 个赞
    def solution() -> list:
        deck_lis = []
        lis = [f"{i} {j}" for i in "CDHS" for j in range(1,14)]
        x = 51
        while x > -1:
            y = random.randint(0,x)
            deck_lis.append(lis.pop(y))
            x -= 1
        return deck_lis
    deck1 = solution()
    deck2 = solution()
    assert len(set(deck1)) == 52
    assert len(set(deck2)) == 52
    assert deck1 != deck2
    assert sorted(deck1) == sorted(deck2)
绝密
def solution() -> list:
    lis = [f"{i} {j}" for i in "CDHS" for j in range(1, 14)]
    random.shuffle(lis)
    return lis
import random
def solution() -> list:
    numbers = [x for x in range(13)]
    card_names = ['H','C','D','S']
    random.shuffle(card_names)

    result = []
    for card_name in card_names:
        random.shuffle(numbers)
        for number in numbers:
            result.append(f'{card_name} {number}')
    return result
import random


def solution() -> list:
    # your code here
    deck = []
    deck_a = ["H", "C", "D", "S"]
    deck_b = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
    while len(deck) < 52:
        one = " ".join([deck_a[random.randint(0, 3)], deck_b[random.randint(0, 12)]])
        if one not in deck:
            deck.append(one)
    print(deck)
    return deck


deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
import random
deck =[]
deck1 = ['H','C','D','S']
for j in range(1,12):
    for i in deck1:
        deck.append(i+str(j))
random.shuffle(deck)
print(deck)
import random

def solution() -> list:

    color_list = ["H", "C", "D", "S"]
    card_list = []
    for color in color_list:
        for i in range(1,14):
            card_list.append(color+str(i))
    random.shuffle(card_list)
    return card_list


deck1 = solution()
print(f"deck1为:{deck1}")
deck2 = solution()
print(f"deck2为:{deck2}")

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
1 个赞
def shuffled_deck():
    from random import shuffle
    arr = [f'{x} {i}' for x in "HCDS" for i in range(1, 14)]
    shuffle(arr)
    return arr
import random
def random_card():
    hs = random.choice(['H', 'C', 'D', 'S'])
    ds = random.choice([i + 1 for i in range(13)])
    s = hs + " " + str(ds)
    return s
def solution() -> list:
    li = []
    for i in range(52):
        while True:
            s = random_card()
            if s not in li:
                li.append(s)
                break
    return li

deck1 = solution()
deck2 = solution()
assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)`预先格式化的文本`
import random


def solution() -> list:
    # your code here
    list1 = [f"{i}{j}" for i in 'HCDS' for j in range(1, 14)]
    random.shuffle(list1)
    return list1


deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
import random


def solution() -> list:
    list1 = [(i, j) for i in 'HCDS' for j in range(1, 14)]
    random.shuffle(list1)
    return list1


deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)
import random
def solution() -> list:
    # H表示红桃:heart:、C表示梅花:clubs:、D表示:diamonds:、S表示:spades
    card_list = []
    flower_list = ['H', 'C', 'D', 'S']
    num_list = [i for i in range(1, 14)]

    random.shuffle(flower_list)
    random.shuffle(num_list)

    for flower in flower_list:
        for num in num_list:
            card_list.append(f"{flower} {num}")

    return card_list
POP_POKE = []
def solution() -> list:
    poke_list = []
    color_list = ["H","C","D","S"]

    for num in range(1,14):
        for color in color_list:
            poke_list.append((color,num))
                
    #保证每次游戏,生成的扑克牌不重复
    while True:
        random.shuffle(poke_list)
        if poke_list not in POP_POKE:
            POP_POKE.append(poke_list)
            return poke_list
        continue


deck1 = solution()
deck2 = solution()

assert len(set(deck1)) == 52
assert len(set(deck2)) == 52
assert deck1 != deck2
assert sorted(deck1) == sorted(deck2)