刘谦魔术栈模拟

import random
import time

class MagicCard(object):
    def __init__(self,*card):
        #1. tear the cards
        self.cards = [*card]*2

    def prove(self):
        #2. insert the top to the bottom
        n = random.randint(2,7)
        self.cards = self.cards[n:]+self.cards[:n]

        #3. insert the top to the middle
        top_three,self.cards =self.cards[:3],self.cards[3:]
        for card in top_three:
            pos = random.randint(1,len(self.cards)-2)
            self.cards.insert(pos,card)

        #4. pop the stack
        temp = self.cards.pop(0)

        # 5. northern or southern people
        num = random.randint(1,3)
        tops,self.cards = self.cards[:num],self.cards[num:]
        for card in tops:
            pos = random.randint(1,len(self.cards)-2)
            self.cards.insert(pos,card)

        #6. sex
        sex_num = random.randint(1,2)
        self.cards[sex_num:]

        #7. move the cards to the bottom of stack constantly
        for _ in range(len(self.cards)):
            self.cards.append(self.cards.pop(0))

        #8. keep luck and throw annoyment
        while(len(self.cards)>1):
            self.cards.append(self.cards.pop(0)) # keep luck
            self.cards.pop(0) # throw annoyment

        return temp, self.cards[0]


positive_num = 0
iters = 1000
for _ in range(iters):
    cle = MagicCard('A', 'B', 'C','D')
    temp,last = cle.prove()
    print(temp,'-->',last)
    time.sleep(0.01)
    if temp is last:
        positive_num += 1

odds = positive_num / iters
print('the odds to meet you is {}%'.format((int(odds*100))))

if odds==1:
    time.sleep(2)
    print('we are destined to meet each other')

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值