【问题标题】:How do I add numbers to each subset in Wolfram mathematica?如何将数字添加到 Wolfram mathematica 中的每个子集?
【发布时间】:2021-09-16 17:44:04
【问题描述】:

假设我有一副 52 张牌。我现在从牌组中取出两张牌,比如说钻石 2 和 4。我现在创建一个子集 {5},这意味着我打印剩下的 50 张牌中的每 5 张牌组合。

我现在如何将钻石 2 和 4 添加到每个子集中,即每组 5 张卡片组合?

【问题讨论】:

  • 到目前为止你尝试过什么?你被困在哪里了?这甚至与编程有关吗?

标签: wolfram-mathematica mathematical-optimization discrete-mathematics mathematical-expressions mathematica-8


【解决方案1】:

更新使用资源函数PlayingCardGraphic生成图形

转换为正确的格式有点麻烦,如果排名以不同的方式生成会更容易。

newHands = 
 Subsets[deckPart, {5}] // Map[Join[#, cardsToRemove] & /* RandomSample];

hand = newHands // RandomChoice

(hand // 
    Map[StringSplit[#, 
       x : DigitCharacter .. | {"J", "Q", "K", "A"} :> x] &]) /. {v_, 
    s_} :> {If[MemberQ[{"J", "Q", "K", "A"}, v], v, ToExpression@v], 
    s} // 
 ResourceFunction["PlayingCardGraphic"][#, "CardSpreadAngle" -> .8] &


5张卡片子集的个数是

50!/(5! 45! = 2118760

在我机器上的笔记本上计算大约需要 2.8 秒,在笔记本上显示大约需要 10 秒。

suits = ToString /@ {\[SpadeSuit], \[HeartSuit], \[DiamondSuit], \[ClubSuit]}
ranks = CharacterRange["2", "9"]~Join~{"10", "J", "Q", "K", "A"}

deck = Outer[StringJoin, ranks, suits] // Flatten // RandomSample

cardsToRemove = {"2\[DiamondSuit]", "4\[DiamondSuit]"}

deckPart = deck // DeleteCases[Alternatives @@ cardsToRemove]

Subsets[deckPart, {5}] // Map[Join[#, cardsToRemove] & /* RandomSample]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多